imageView.setImageResource错误和遗憾(app)已经停止

时间:2014-06-09 14:50:56

标签: android imageview

说到GridView页面,我在应用程序中遇到错误 在logcat“imageview.setımageresource”中查看错误。 但我找不到解决方案。

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public Integer[] birdPicture = { 
            R.drawable.bird1,
            R.drawable.bird2,
            R.drawable.bird3,
            R.drawable.bird4,
            R.drawable.bird5,
            R.drawable.bird6,
            R.drawable.bird7

    };

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

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

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

    @Override
    public long getItemId(int position) {
        return birdPicture[position];
    }

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

        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(birdPicture[position]);
        Resources r = Resources.getSystem();
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                90, r.getDisplayMetrics());
        ;
        imageView
                .setLayoutParams(new GridView.LayoutParams((int) px, (int) px));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        return imageView;
    }

}

logCat在这里工作

    06-09 17:48:59.996: E/AndroidRuntime(29106): java.lang.OutOfMemoryError
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.content.res.Resources.loadDrawable(Resources.java:3024)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.content.res.Resources.getDrawable(Resources.java:1586)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.widget.ImageView.resolveUri(ImageView.java:648)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.widget.ImageView.setImageResource(ImageView.java:377)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at com.example.proje1.ImageAdapter.getView(ImageAdapter.java:165)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.widget.AbsListView.obtainView(AbsListView.java:2694)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.widget.GridView.makeAndAddView(GridView.java:1350)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.widget.GridView.makeRow(GridView.java:345)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.widget.GridView.fillDown(GridView.java:287)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.widget.GridView.fillGap(GridView.java:247)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:6682)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:5687)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.view.Choreographer.doCallbacks(Choreographer.java:603)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.view.Choreographer.doFrame(Choreographer.java:572)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.os.Handler.handleCallback(Handler.java:733)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.os.Looper.loop(Looper.java:157)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at android.app.ActivityThread.main(ActivityThread.java:5356)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at java.lang.reflect.Method.invokeNative(Native Method)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at java.lang.reflect.Method.invoke(Method.java:515)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-09 17:48:59.996: E/AndroidRuntime(29106):    at dalvik.system.NativeStart.main(Native Method)
06-09 17:49:02.301: I/Process(29106): Sending signal. PID: 29106 SIG: 9

1 个答案:

答案 0 :(得分:0)

图像很大,很多都要显示。请参阅例外java.lang.OutOfMemoryError

在将Bitmap加载到视图中之前,需要调整其大小。见http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

这似乎对我有用。

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class ImageAdapter
    extends BaseAdapter
{
    public Integer[] birdPicture = {
        R.drawable.bird1,
        R.drawable.bird2,
        R.drawable.bird3,
        R.drawable.bird4,
        R.drawable.bird5,
        R.drawable.bird6,
        R.drawable.bird7
    };

    private int mItemSizePx;

    public ImageAdapter(Context context)
    {
        mItemSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90.0f,
            context.getResources().getDisplayMetrics());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ImageView imageView;
        if(convertView == null){
            imageView = new ImageView(parent.getContext());
            imageView.setLayoutParams(new GridView.LayoutParams(mItemSizePx, mItemSizePx));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        }
        else{
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(birdPicture[position]);
        return imageView;
    }

    @Override
    public Integer getItem(int position)
    {
        return birdPicture[position];
    }

    @Override
    public long getItemId(int position)
    {
        return birdPicture[position].longValue();
    }

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

如果您遇到问题,请确保将您的drawable放在正确的目录中。