android gridview crashes on Galaxy 3

谁说我不能喝 提交于 2019-11-29 07:09:06

try to replace fill_parent with match_parent because according to my information the attribute value fill_parent is deprecated on android 2.2 and above hope this would help

You can see this link http://developer.android.com/training/displaying-bitmaps/load-bitmap.html I hope this will help you

AS for the question "why the hell is it crashing on galaxy 3 and not on inferior phones", the answer is "I have no clue". But, turns out I have been misusing the gridview. It is a bad idea to use the same image both for the thumbnail and for the whole image. What I did (which might be nothing more than a workaround, but it works) is to use 2 sets of images, small one and full size ines. This is done like this

For setting the thumbnails:

private Integer[] mThumbIds = {
        R.drawable.third_world__small , R.drawable.annoyinggirl__small,
        R.drawable.asianfather__small, R.drawable.awkawespeng__small,
        R.drawable.awkpeng__small, R.drawable.blankdad__small,
        R.drawable.collegesenior__small, R.drawable.courage__small,
        R.drawable.frog__small, R.drawable.frynotsure__small,
        R.drawable.goodguygreg__small, R.drawable.scumbag__small,
        R.drawable.raptor__small,R.drawable.keano__small,
        R.drawable.successkid__small, R.drawable.wonka__small,
        R.drawable.braceyourselves__small, R.drawable.onedoesnotsimply__small,
        R.drawable.firstworldproblem__small, R.drawable.amitheonlyone__small,
        R.drawable.badluckbrian__small, R.drawable.interestingman__small,
        R.drawable.toodamnhigh__small, R.drawable.def__small    
};

public View getView(int position, View convertView, ViewGroup parent) {
    //clearAllResources();
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mWidth, mHeight));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

Then, create another array:

private Map<Integer, Integer> mThumbIdToFullSizeId = new HashMap<Integer,Integer>();
public ImageAdapter(Context c, int width, int height) {
    mContext = c;
    mWidth = width;
    mHeight = height;
    mThumbIdToFullSizeId.put(R.drawable.third_world__small, R.drawable.third_world);
    mThumbIdToFullSizeId.put(R.drawable.annoyinggirl__small,R.drawable.annoyinggirl);
    mThumbIdToFullSizeId.put(R.drawable.asianfather__small, R.drawable.asianfather);
    mThumbIdToFullSizeId.put(R.drawable.awkawespeng__small, R.drawable.awkawespeng);
    mThumbIdToFullSizeId.put(R.drawable.awkpeng__small, R.drawable.awkpeng);
    mThumbIdToFullSizeId.put(R.drawable.blankdad__small, R.drawable.blankdad);
    mThumbIdToFullSizeId.put(R.drawable.collegesenior__small, R.drawable.collegesenior);
    mThumbIdToFullSizeId.put(R.drawable.courage__small, R.drawable.courage);
    mThumbIdToFullSizeId.put(R.drawable.frog__small, R.drawable.frog);
    mThumbIdToFullSizeId.put(R.drawable.frynotsure__small, R.drawable.frynotsure);
    mThumbIdToFullSizeId.put(R.drawable.goodguygreg__small, R.drawable.goodguygreg);
    mThumbIdToFullSizeId.put(R.drawable.scumbag__small, R.drawable.scumbag);
    mThumbIdToFullSizeId.put(R.drawable.raptor__small, R.drawable.raptor);
    mThumbIdToFullSizeId.put(R.drawable.keano__small, R.drawable.keano);
    mThumbIdToFullSizeId.put(R.drawable.successkid__small, R.drawable.successkid);
    mThumbIdToFullSizeId.put(R.drawable.wonka__small, R.drawable.wonka);
    mThumbIdToFullSizeId.put(R.drawable.braceyourselves__small, R.drawable.braceyourselves);
    mThumbIdToFullSizeId.put(R.drawable.onedoesnotsimply__small, R.drawable.onedoesnotsimply);
    mThumbIdToFullSizeId.put(R.drawable.firstworldproblem__small, R.drawable.firstworldproblem);
    mThumbIdToFullSizeId.put(R.drawable.amitheonlyone__small, R.drawable.amitheonlyone);
    mThumbIdToFullSizeId.put(R.drawable.badluckbrian__small, R.drawable.badluckbrian);
    mThumbIdToFullSizeId.put(R.drawable.interestingman__small, R.drawable.interestingman);
    mThumbIdToFullSizeId.put(R.drawable.toodamnhigh__small, R.drawable.toodamnhigh);
    mThumbIdToFullSizeId.put(R.drawable.def__small, R.drawable.def);
}

Then, for the getview function:

public long getItemId(int position) {

    return mThumbIdToFullSizeId.get(mThumbIds[position]);
}

And everything will be much more memory friendly. I'm giving the checkmark to Muhannad for giving me the link to http://developer.android.com/training/displaying-bitmaps/load-bitmap.html which helped alot

I had a similar problem. My app always run good without any problem but started crashing with OutOfMemory error on devices with Android 4. In particular, it crashes when loading the GalleryView adapter. After reading many pages of the offical documentation (http://android.developer.com) I learned that the memory is used differently from the old version of SO but definitively solved the problem working on the manifest file. I set Android 3.0 as Project Build target, added android:targetSdkVersion="15", added android:largeHeap="true" to the application tag and finally added supports-screens android:anyDensity="false"

Hope this can help you too!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!