Android的setBackgroundResource造成内存溢出excepiton内存、Android、setBackgroundResource、excepiton

2023-09-12 11:13:35 作者:逆光

我工作的一个gamebook应用程序,显示12次在ViewPager。 这是我的自定义PagerAdapter:

I'm working on a gamebook application that shows 12 views in a ViewPager. This is my custom PagerAdapter :

private class ImagePagerAdapter extends PagerAdapter {

    private int[] mImages = new int[] { R.drawable.copertinai,
            R.drawable.blui, R.drawable.azzurroi, R.drawable.rossoi,
            R.drawable.gialloi, R.drawable.verdei, R.drawable.rosai,
            R.drawable.grigioi, R.drawable.neroi, R.drawable.arancionei,
            R.drawable.marronei, R.drawable.violai, R.drawable.ulm };

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Context context = MainActivity.this;
        RelativeLayout relLayImageView = new RelativeLayout(context);
        relLayImageView.setBackgroundResource(mImages[position]);

        ((ViewPager) container).addView(relLayImageView, new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        return relLayImageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((RelativeLayout) object);
        object=null; 
        System.gc();
    }
}

在某些设备上它会导致出现内存不足的exceptionr随机在这条线的code被称为

In some devices it cause an out of memory exceptionr randomly when this line of code is called

relLayImageView.setBackgroundResource(mImages[position]);

但我看到这样的事情在logcat中,当我打开网页上的所有设备:

But in all devices I see something like this in the logcat when i turn pages:

12-31 00:25:31.655: I/dalvikvm-heap(9767): Grow heap (frag case) to 50.875MB for 10384016-byte allocation

应用程序也崩溃在某些设备进行了同样的问题时,在另一个活动我设置不同的背景资源,基于用户行为的主要布局。在这里,code:

The app also crashes in some devices for the same problem when in another Activity I set different background resource to the main layout based on user action. Here the code:

            btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                colorButtons.get(indiceColoreAttuale).setBackgroundResource(
                                unSelectedColorsRes[indiceColoreAttuale]);

                switch (index) {
                case 0:
                    mainLayout.setBackgroundResource(R.drawable.blus);
                    break;
                case 1:
                    mainLayout
                            .setBackgroundResource(R.drawable.azzurros); 
                    break;
                case 2:
                    mainLayout
                            .setBackgroundResource(R.drawable.rossos);
                    break;
                case 3:
                    mainLayout
                            .setBackgroundResource(R.drawable.giallos);
                    break;
                case 4:
                    mainLayout
                            .setBackgroundResource(R.drawable.verdes);
                    break;
                case 5:
                    mainLayout
                            .setBackgroundResource(R.drawable.rosas);
                    break;
                case 6:                     
                    mainLayout
                            .setBackgroundResource(R.drawable.grigios);
                    break;
                case 7:
                    mainLayout
                            .setBackgroundResource(R.drawable.neros);
                    break;
                case 8:
                    mainLayout
                            .setBackgroundResource(R.drawable.arancios);
                    break;
                case 9:
                    mainLayout
                            .setBackgroundResource(R.drawable.marrones);
                    break;
                case 10:
                    mainLayout
                            .setBackgroundResource(R.drawable.violas);
                    break;
                }

                mainLayout.startAnimation(animationShowTextColor);
                mainLayout.setGravity(Gravity.CENTER_HORIZONTAL);
                indiceColoreAttuale = index;
                colorButtons.get(index).setBackgroundResource(
                        selectedColorsRes[index]);

            }
        });

据excepiton当我再次打电话setBackgroundResource()上mainLayout运行。

It runs excepiton again when I call setBackgroundResource() on mainLayout.

我希望你能帮助我解决这个问题,先谢谢了!

I hope you can help me to solve this, thanks in advance!

推荐答案

我解决了!所有的线索都不错,但真正的问题是/绘制文件夹!我把所有的图片在被认为是由系统像/绘制/ MDPI/绘制一般文件夹,所以当我在华电国际或多个图像的设备正在运行进行了调整,并成为太大引起OutOfMemoryException异常!

I solved! All your hints were good but the real problem was the "/drawable" folder! I had all the pictures in "/drawable" generic folder that is considered by the system like "/drawable/mdpi", so when I were running in devices with hdpi or more the images were resized, and became too big which cause OutOfMemoryException!

现在我使用/绘制-nodpi来存储我的图片。此文件夹的作品,如/绘制但图像不会被调整!

Now i'm using "/drawable-nodpi" to store my images. This folder works like "/drawable" but the images are never resized!