GridView示例无法正常工作

时间:2014-02-22 10:35:59

标签: android android-layout gridview android-logcat

我正在从这个链接中获取帮助:Android GridView Layout Tutorial。当我运行我的应用时,我的应用停止工作。我收到了以下错误。 我想创建gridview,在gridview中显示缩略图图像,在点击时显示全尺寸图像。为此,我在drawable中保存每个图像的缩略图和完整图像。这该怎么做?请帮助我。

ImageAdapter类 -

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public Integer[] mThumbIds = {
            R.drawable.kri1, R.drawable.kri2,
            R.drawable.kri3, R.drawable.kri4,
            R.drawable.kri5, R.drawable.kri6,
            R.drawable.kri7, R.drawable.kri8,
            R.drawable.kri9,R.drawable.kri7, R.drawable.kri8,
            R.drawable.kri9, R.drawable.kri7, R.drawable.kri8,
            R.drawable.kri9
    };

    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
    }

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

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;
    }

}

的logcat -

02-20 07:06:19.930: E/AndroidRuntime(2541): FATAL EXCEPTION: main
02-20 07:06:19.930: E/AndroidRuntime(2541): Process: com.example.gridview, PID: 2541
02-20 07:06:19.930: E/AndroidRuntime(2541): java.lang.OutOfMemoryError
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:575)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:410)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.content.res.Resources.loadDrawable(Resources.java:2110)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.content.res.Resources.getDrawable(Resources.java:700)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.widget.ImageView.resolveUri(ImageView.java:638)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.widget.ImageView.setImageResource(ImageView.java:367)

1 个答案:

答案 0 :(得分:0)

您正在使用高分辨率图像,因此它可以在堆上提供很大的负载。而是尝试在getView()上对您正在使用的图像进行采样。

BitmapFactory.Options options = new BitmapFactory.Options();
// set options for your need here
BitmapFactory.decodeResource(context, yourImageId, options);