java.lang.OutOfMemoryError setBackgroundResource

时间:2013-10-28 11:48:26

标签: java android drawable out-of-memory setbackground

我有不同的按钮用于在imageview中加载图像。 当用户点击第一个按钮10个图像被加载时,用户点击第二个按钮加载更多图像10,而其他10个图像都没有显示,但是当他点击第三个按钮加载其他10个图片时我得到java.lang .OutOfMemoryError。我怎么能摆脱这个问题?我在这里阅读了很多帖子,我尝试了各种方法,比如使用位图,然后回收位图,但是我给出了同样的错误。我不希望在用户点击按钮时删除图片,因为它们必须多次重复使用。有没有办法把图像放在内存中?还是重置内存缓存?先帮助我,谢谢。此代码适用于Android版本2.3.3,但在版本4.1崩溃  这是代码:

ImageView  
    imageView1n,imageView2n,imageView3n,imageView4n,imageView5n,imageView6n,imageView7n,imageView8n,imageView9n,imageView10n,
    imageView1b,imageView2b,imageView3b,imageView4b,imageView5b,imageView6b,imageView7b,imageView8b,imageView9b,imageView10b,
imageView1l,imageView2l,imageView3l,imageView4l,imageView5l,imageView6l,imageView7l,imageView8l,imageView9l,imageView10l;






protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        imageView1n=(ImageView)findViewById(R.id.imageView1n);

        imageView2n=(ImageView)findViewById(R.id.imageView2n);

        imageView3n=(ImageView)findViewById(R.id.imageView3n);

        imageView4n=(ImageView)findViewById(R.id.imageView4n);

        imageView5n=(ImageView)findViewById(R.id.imageView5n);

        imageView6n=(ImageView)findViewById(R.id.imageView6n);

        imageView7n=(ImageView)findViewById(R.id.imageView7n);

        imageView8n=(ImageView)findViewById(R.id.imageView8n);

        imageView9n=(ImageView)findViewById(R.id.imageView9n);

        imageView10n=(ImageView)findViewById(R.id.imageView10n);

imageView1b=(ImageView)findViewById(R.id.imageView1b);

        imageView2b=(ImageView)findViewById(R.id.imageView2b);

        imageView3b=(ImageView)findViewById(R.id.imageView3b);

        imageView4b=(ImageView)findViewById(R.id.imageView4b);

        imageView5b=(ImageView)findViewById(R.id.imageView5b);

        imageView6b=(ImageView)findViewById(R.id.imageView6b);

        imageView7b=(ImageView)findViewById(R.id.imageView7b);

        imageView8b=(ImageView)findViewById(R.id.imageView8b);

        imageView9b=(ImageView)findViewById(R.id.imageView9b);

        imageView10b=(ImageView)findViewById(R.id.imageView10b);


imageView1l=(ImageView)findViewById(R.id.imageView1l);

        imageView2l=(ImageView)findViewById(R.id.imageView2l);

        imageView3l=(ImageView)findViewById(R.id.imageView3l);

        imageView4l=(ImageView)findViewById(R.id.imageView4l);

        imageView5l=(ImageView)findViewById(R.id.imageView5l);

        imageView6l=(ImageView)findViewById(R.id.imageView6l);

        imageView7l=(ImageView)findViewById(R.id.imageView7l);

        imageView8l=(ImageView)findViewById(R.id.imageView8l);

        imageView9l=(ImageView)findViewById(R.id.imageView9l);

        imageView10l=(ImageView)findViewById(R.id.imageView10l);    






 Button button1 = (Button)findViewById(R.id.Bopenbx);
                       button1.setOnClickListener(new Button.OnClickListener(){
                          @SuppressWarnings("deprecation")
                        @Override
                           public void onClick(View arg1) {
                            // TODO Auto-generated method stub

                             MediaPlayer playSuccess = MediaPlayer.create(getBaseContext(),R.raw.button_opengohan1);


                            playSuccess.start();
                            playSuccess.setOnCompletionListener(new OnCompletionListener() {
                                @Override
                                public void onCompletion(MediaPlayer playSuccess) {
                                    playSuccess.reset();
                                }
                            });






                   Button button1b = (Button)findViewById(R.id.Bopenbx);
                   button1b.setBackgroundResource(R.drawable.gohan_open1);
                   Button button2b = (Button)findViewById(R.id.Bopennx);
                   button2b.setBackgroundResource(R.drawable.vegeta_open0);
                   Button button3b = (Button)findViewById(R.id.Bopenlx);
                   button3b.setBackgroundResource(R.drawable.goku_open0);




 imageView1b.setBackgroundResource(R.drawable.gohan_1);
                      imageView2b.setBackgroundResource(R.drawable.gohan_2);
                      imageView3b.setBackgroundResource(R.drawable.gohan_3);
                      imageView4b.setBackgroundResource(R.drawable.gohan_4);
                      imageView5b.setBackgroundResource(R.drawable.gohan_5);
                      imageView6b.setBackgroundResource(R.drawable.gohan_6);
                      imageView7b.setBackgroundResource(R.drawable.gohan_7);
                      imageView8b.setBackgroundResource(R.drawable.gohan_8);
                      imageView9b.setBackgroundResource(R.drawable.gohan_9);
                      imageView10b.setBackgroundResource(R.drawable.gohan_10);




imageView1b.setVisibility(View.VISIBLE);
                  imageView2b.setVisibility(View.VISIBLE);
                  imageView3b.setVisibility(View.VISIBLE);
                  imageView4b.setVisibility(View.VISIBLE);
                  imageView5b.setVisibility(View.VISIBLE);
                  imageView6b.setVisibility(View.VISIBLE);
                  imageView7b.setVisibility(View.VISIBLE);
                  imageView8b.setVisibility(View.VISIBLE);
                  imageView9b.setVisibility(View.VISIBLE);
                  imageView10b.setVisibility(View.VISIBLE);


imageView1n.setImageDrawable(null);
                imageView2n.setImageDrawable(null);
                imageView3n.setImageDrawable(null);
                imageView4n.setImageDrawable(null);
                imageView5n.setImageDrawable(null);
                imageView6n.setImageDrawable(null);
                imageView7n.setImageDrawable(null);
                imageView8n.setImageDrawable(null);
                imageView9n.setImageDrawable(null);
                imageView10n.setImageDrawable(null);
                imageView1l.setImageDrawable(null);
                imageView2l.setImageDrawable(null);
                imageView3l.setImageDrawable(null);
                imageView4l.setImageDrawable(null);
                imageView5l.setImageDrawable(null);
                imageView6l.setImageDrawable(null);
                imageView7l.setImageDrawable(null);
                imageView8l.setImageDrawable(null);
                imageView9l.setImageDrawable(null);
                imageView10l.setImageDrawable(null);
imageView1l.setVisibility(View.GONE);
                    imageView2l.setVisibility(View.GONE);
                    imageView3l.setVisibility(View.GONE);
                    imageView4l.setVisibility(View.GONE);
                    imageView5l.setVisibility(View.GONE);
                    imageView6l.setVisibility(View.GONE);
                    imageView7l.setVisibility(View.GONE);
                    imageView8l.setVisibility(View.GONE);
                    imageView9l.setVisibility(View.GONE);
                    imageView10l.setVisibility(View.GONE);

                     imageView1n.setVisibility(View.GONE);
                    imageView2n.setVisibility(View.GONE);
                    imageView3n.setVisibility(View.GONE);
                    imageView4n.setVisibility(View.GONE);
                    imageView5n.setVisibility(View.GONE);
                    imageView6n.setVisibility(View.GONE);
                    imageView7n.setVisibility(View.GONE);
                    imageView8n.setVisibility(View.GONE);
                    imageView9n.setVisibility(View.GONE);
                    imageView10n.setVisibility(View.GONE);

}







                       });




Button button2=(Button) findViewById(R.id.Bopenlx);
                       button2.setOnClickListener(new Button.OnClickListener(){
                          @Override
                           public void onClick(View arg2) {
                            // TODO Auto-generated method stub


                             MediaPlayer playSuccess = MediaPlayer.create(getBaseContext(),R.raw.button_opengoku1);


                            playSuccess.start();
                            playSuccess.setOnCompletionListener(new OnCompletionListener() {
                                @Override
                                public void onCompletion(MediaPlayer playSuccess) {
                                    playSuccess.reset();
                                }
                            });        










                              Button button1l = (Button)findViewById(R.id.Bopenbx);
                              button1l.setBackgroundResource(R.drawable.gohan_open0);
                              Button button2l = (Button)findViewById(R.id.Bopennx);
                              button2l.setBackgroundResource(R.drawable.vegeta_open0);
                              Button button3l = (Button)findViewById(R.id.Bopenlx);
                              button3l.setBackgroundResource(R.drawable.goku_open1);



 imageView1l.setBackgroundResource(R.drawable.goku_1);
                            imageView2l.setBackgroundResource(R.drawable.goku_2);
                            imageView3l.setBackgroundResource(R.drawable.goku_3);
                            imageView4l.setBackgroundResource(R.drawable.goku_4);
                            imageView5l.setBackgroundResource(R.drawable.goku_5);
                            imageView6l.setBackgroundResource(R.drawable.goku_6);
                            imageView7l.setBackgroundResource(R.drawable.goku_7);
                            imageView8l.setBackgroundResource(R.drawable.goku_8);
                            imageView9l.setBackgroundResource(R.drawable.goku_9);
                            imageView10l.setBackgroundResource(R.drawable.goku_10);


imageView1l.setVisibility(View.VISIBLE);
                                imageView2l.setVisibility(View.VISIBLE);
                                imageView3l.setVisibility(View.VISIBLE);
                                imageView4l.setVisibility(View.VISIBLE);
                                imageView5l.setVisibility(View.VISIBLE);
                                imageView6l.setVisibility(View.VISIBLE);
                                imageView7l.setVisibility(View.VISIBLE);
                                imageView8l.setVisibility(View.VISIBLE);
                                imageView9l.setVisibility(View.VISIBLE);
                                imageView10l.setVisibility(View.VISIBLE);


imageView1b.setImageDrawable(null);
                            imageView2b.setImageDrawable(null);
                            imageView3b.setImageDrawable(null);
                            imageView4b.setImageDrawable(null);
                            imageView5b.setImageDrawable(null);
                            imageView6b.setImageDrawable(null);
                            imageView7b.setImageDrawable(null);
                            imageView8b.setImageDrawable(null);
                            imageView9b.setImageDrawable(null);
                            imageView10b.setImageDrawable(null);
                            imageView1n.setImageDrawable(null);
                            imageView2n.setImageDrawable(null);
                            imageView3n.setImageDrawable(null);
                            imageView4n.setImageDrawable(null);
                            imageView5n.setImageDrawable(null);
                            imageView6n.setImageDrawable(null);
                            imageView7n.setImageDrawable(null);
                            imageView8n.setImageDrawable(null);
                            imageView9n.setImageDrawable(null);
                            imageView10n.setImageDrawable(null);

 imageView1b.setVisibility(View.GONE);
                              imageView2b.setVisibility(View.GONE);
                              imageView3b.setVisibility(View.GONE);
                              imageView4b.setVisibility(View.GONE);
                              imageView5b.setVisibility(View.GONE);
                              imageView6b.setVisibility(View.GONE);
                              imageView7b.setVisibility(View.GONE);
                              imageView8b.setVisibility(View.GONE);
                              imageView9b.setVisibility(View.GONE);
                              imageView10b.setVisibility(View.GONE);
                              imageView1n.setVisibility(View.GONE);
                              imageView2n.setVisibility(View.GONE);
                              imageView3n.setVisibility(View.GONE);
                              imageView4n.setVisibility(View.GONE);
                              imageView5n.setVisibility(View.GONE);
                              imageView6n.setVisibility(View.GONE);
                              imageView7n.setVisibility(View.GONE);
                              imageView8n.setVisibility(View.GONE);
                              imageView9n.setVisibility(View.GONE);
                              imageView10n.setVisibility(View.GONE);
   }    



                       });



 Button button3=(Button) findViewById(R.id.Bopennx);
                       button3.setOnClickListener(new Button.OnClickListener(){
                          @Override
                           public void onClick(View arg3) {
                            // TODO Auto-generated method stub


                             MediaPlayer playSuccess = MediaPlayer.create(getBaseContext(),R.raw.button_openvegeta1);


                            playSuccess.start();
                            playSuccess.setOnCompletionListener(new OnCompletionListener() {
                                @Override
                                public void onCompletion(MediaPlayer playSuccess) {
                                    playSuccess.reset();
                                }
                            });







                            Button button1n = (Button)findViewById(R.id.Bopenbx);
                            button1n.setBackgroundResource(R.drawable.gohan_open0);
                            Button button2n = (Button)findViewById(R.id.Bopennx);
                            button2n.setBackgroundResource(R.drawable.vegeta_open1);
                            Button button3n = (Button)findViewById(R.id.Bopenlx);
                            button3n.setBackgroundResource(R.drawable.goku_open0);





imageView1n.setBackgroundResource(R.drawable.vegeta_1);
                            imageView2n.setBackgroundResource(R.drawable.vegeta_2);
                            imageView3n.setBackgroundResource(R.drawable.vegeta_3);
                            imageView4n.setBackgroundResource(R.drawable.vegeta_4);
                            imageView5n.setBackgroundResource(R.drawable.vegeta_5);
                            imageView6n.setBackgroundResource(R.drawable.vegeta_6);
                            imageView7n.setBackgroundResource(R.drawable.vegeta_7);
                            imageView8n.setBackgroundResource(R.drawable.vegeta_8);
                            imageView9n.setBackgroundResource(R.drawable.vegeta_9);
                            imageView10n.setBackgroundResource(R.drawable.vegeta_10);







                            imageView1n.setVisibility(View.VISIBLE);
                            imageView2n.setVisibility(View.VISIBLE);
                            imageView3n.setVisibility(View.VISIBLE);
                            imageView4n.setVisibility(View.VISIBLE);
                            imageView5n.setVisibility(View.VISIBLE);
                            imageView6n.setVisibility(View.VISIBLE);
                            imageView7n.setVisibility(View.VISIBLE);
                            imageView8n.setVisibility(View.VISIBLE);
                            imageView9n.setVisibility(View.VISIBLE);
                            imageView10n.setVisibility(View.VISIBLE);



imageView1b.setImageDrawable(null);
                            imageView2b.setImageDrawable(null);
                            imageView3b.setImageDrawable(null);
                            imageView4b.setImageDrawable(null);
                            imageView5b.setImageDrawable(null);
                            imageView6b.setImageDrawable(null);
                            imageView7b.setImageDrawable(null);
                            imageView8b.setImageDrawable(null);
                            imageView9b.setImageDrawable(null);
                            imageView10b.setImageDrawable(null);
                            imageView1l.setImageDrawable(null);
                            imageView2l.setImageDrawable(null);
                            imageView3l.setImageDrawable(null);
                            imageView4l.setImageDrawable(null);
                            imageView5l.setImageDrawable(null);
                            imageView6l.setImageDrawable(null);
                            imageView7l.setImageDrawable(null);
                            imageView8l.setImageDrawable(null);
                            imageView9l.setImageDrawable(null);
                            imageView10l.setImageDrawable(null);



                                imageView1b.setVisibility(View.GONE);
                                imageView2b.setVisibility(View.GONE);
                                imageView3b.setVisibility(View.GONE);
                                imageView4b.setVisibility(View.GONE);
                                imageView5b.setVisibility(View.GONE);
                                imageView6b.setVisibility(View.GONE);
                                imageView7b.setVisibility(View.GONE);
                                imageView8b.setVisibility(View.GONE);
                                imageView9b.setVisibility(View.GONE);
                                imageView10b.setVisibility(View.GONE);
                                imageView1l.setVisibility(View.GONE);
                                imageView2l.setVisibility(View.GONE);
                                imageView3l.setVisibility(View.GONE);
                                imageView4l.setVisibility(View.GONE);
                                imageView5l.setVisibility(View.GONE);
                                imageView6l.setVisibility(View.GONE);
                                imageView7l.setVisibility(View.GONE);
                                imageView8l.setVisibility(View.GONE);
                                imageView9l.setVisibility(View.GONE);
                                imageView10l.setVisibility(View.GONE);



       }    


                       });

1 个答案:

答案 0 :(得分:-1)

这将解决您的问题:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

并通过以下方式释放额外的内存调用垃圾收集器:System.gc();