0

画像のギャラリーを作成しようとしています。コードを作成しましたが、すべて問題ありませんが、avd でアプリを実行し、アクティビティを起動するボタンをクリックすると、アプリがクラッシュします。

私のコードは私のxml用です:

<ImageSwitcher android:id="@+id/switcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
/>

<Gallery android:id="@+id/gallery"
    android:background="#55000000"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"

    android:gravity="center_vertical"
    android:spacing="16dp"
/>

</RelativeLayout>

そして私のクラスのために:

public class ScrollingQuran extends Activity implements
AdapterView.OnItemSelectedListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.qurangallery);

    mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
    mSwitcher.setFactory(this);
    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in));
    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));

    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    mSwitcher.setImageResource(mImageIds[position]);
}

public void onNothingSelected(AdapterView<?> parent) {
}

public View makeView() {
    ImageView i = new ImageView(this);
    i.setBackgroundColor(0xFF000000);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    return i;
}

private ImageSwitcher mSwitcher;

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(mThumbIds[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     //   i.setBackgroundResource(R.drawable.picture_frame);
        return i;
    }

    private Context mContext;

}

private Integer[] mThumbIds = {
        R.drawable.page1, R.drawable.page2};

private Integer[] mImageIds = {
        R.drawable.page1, R.drawable.page2};

}

何かを見つけられないように見えるので、どこが間違っているのか誰か教えてください。

私の丸太猫:

>12-06 17:54:10.050: E/AndroidRuntime(1152): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fuelfinder/com.example.fuelfinder.ScrollingQuran}: java.lang.ClassCastException: com.example.fuelfinder.ScrollingQuran cannot be cast to android.widget.ViewSwitcher$ViewFactory
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.os.Looper.loop(Looper.java:137)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invoke(Method.java:511)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at dalvik.system.NativeStart.main(Native Method)
12-06 17:54:10.050: E/AndroidRuntime(1152): Caused by: java.lang.ClassCastException: com.example.fuelfinder.ScrollingQuran cannot be cast to android.widget.ViewSwitcher$ViewFactory
12-06 17:54:10.050: E/AndroidRuntime(1152):     at com.example.fuelfinder.ScrollingQuran.onCreate(ScrollingQuran.java:31)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.app.Activity.performCreate(Activity.java:5008)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-06 17:54:10.050: E/AndroidRuntime(1152):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-06 17:54:10.050: E/AndroidRuntime(1152):     ... 11 more
12-06 17:56:08.160: E/Trace(1198): error opening trace file: No such file or directory (2)
4

1 に答える 1

0

正しいレイアウト パラメータをインポートしたかどうかを確認してください。他のものをインポートしている可能性があります..一度見てみましょう

クラス名、xmlファイル、およびそこにある画像を変更してください

package com.exam.samplegalery;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener, ViewFactory {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));

        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemSelectedListener(this);
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
        mSwitcher.setImageResource(mImageIds[position]);
    }

    public void onNothingSelected(AdapterView<?> parent) {
    }

    public View makeView() {
        ImageView i = new ImageView(MainActivity.this);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        return i;
    }

    private ImageSwitcher mSwitcher;

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageResource(mThumbIds[position]);
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            // i.setBackgroundResource(R.drawable.picture_frame);
            return i;
        }

        private Context mContext;

    }

    private Integer[] mThumbIds = { R.drawable.ic_action_search, R.drawable.ic_launcher };

    private Integer[] mImageIds = { R.drawable.ic_action_search, R.drawable.ic_action_search };

}
于 2012-12-06T18:22:26.617 に答える