0

イメージビューに画像をロードするためのさまざまなボタンがあります。ユーザーが最初のボタンをクリックすると、10 個の画像が読み込まれます。ユーザーが 2 番目のボタンをクリックすると、さらに画像が 10 個読み込まれます。他の 10 個の画像は表示されませんが、3 番目のボタンをクリックして他の 10 個の画像を読み込むと、II get 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);



       }    


                       });
4

1 に答える 1

-1

これで問題が解決します: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

余分なメモリを解放するには、次の方法でガベージ コレクターを呼び出します。 System.gc();

于 2013-10-28T12:45:23.270 に答える