0

我正在尝试实现一个画廊,该画廊显示位于外部存储中的特定文件夹中的所有图像。

这是我的代码:

public class ExistingPicGallery extends Activity {

    private Cursor cursor;
    private int columnIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);
        Gallery g = (Gallery) findViewById(R.id.gallery);
        String[] projection = { MediaStore.Images.ImageColumns._ID,
                MediaStore.Images.Thumbnails.IMAGE_ID,
                MediaStore.Images.Thumbnails.KIND };
        // Create the cursor pointing to the SDCard
        String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select
                                                                        // only
                                                                        // mini's
                MediaStore.Images.Thumbnails.MINI_KIND;
        cursor = managedQuery(
                MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection,
                selection, null, null);
        // Get the column index of the image ID
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
        g.setAdapter(new ImageAdapter(this));

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position,
                    long id) {
                Toast.makeText(ExistingPicGallery.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });

    }

    private class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;

        private Context mContext;

        File folder;
        String[] allFiles;

        public ImageAdapter(Context c) {
            mContext = c;
            folder = new File(Environment.getExternalStorageDirectory()
                    .toString() + "/CuantoComic");
            allFiles = folder.list();
            int y = 0;

        }

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

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

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

        public View getView(final int position, View convertView,
                ViewGroup parent) {

            ImageView myImageView = new ImageView(mContext);

            if (convertView != null)
                myImageView = (ImageView) convertView;
            else {
                myImageView = new ImageView(mContext);
                myImageView.setLayoutParams(new GridView.LayoutParams(60, 60));
                myImageView.setAdjustViewBounds(false);
                myImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            }

            Bitmap bitmapImage = BitmapFactory.decodeFile(folder + "/"
                    + allFiles[position]);
            BitmapDrawable drawableImage = new BitmapDrawable(bitmapImage);
            myImageView.setImageDrawable(drawableImage);

            return myImageView;

        }

    }

}

和xml:

<?xml version="1.0" encoding="utf-8"?>

<Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
/>

我在 LogCat 中收到以下错误:

05-14 11:37:50.311: E/AndroidRuntime(6904): FATAL EXCEPTION: main
05-14 11:37:50.311: E/AndroidRuntime(6904): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.widget.Gallery.setUpChild(Gallery.java:769)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.widget.Gallery.makeAndAddView(Gallery.java:738)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.widget.Gallery.layout(Gallery.java:622)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.widget.Gallery.onLayout(Gallery.java:336)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.view.View.layout(View.java:7034)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.view.View.layout(View.java:7034)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.view.View.layout(View.java:7034)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.view.View.layout(View.java:7034)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.os.Looper.loop(Looper.java:144)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at android.app.ActivityThread.main(ActivityThread.java:4937)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at java.lang.reflect.Method.invoke(Method.java:521)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-14 11:37:50.311: E/AndroidRuntime(6904):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

myImageView.setLayoutParams(new GridView.LayoutParams(60, 60));

此行导致 classcastEx。你确定你有

import android.widget.Gallery.LayoutParams;

代替

import android.widget.AbsListView.LayoutParams;

于 2012-05-14T09:47:29.833 回答