android gridview crashes on Galaxy 3

后端 未结 3 1271
迷失自我
迷失自我 2020-12-18 01:37

Ok, I think I have a real question for a change. I implemented a gridView in Android, following step by step the instructions in the Android Developers page, http://develope

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-18 02:24

    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 mThumbIdToFullSizeId = new HashMap();
    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

提交回复
热议问题