0
我有不同的按鈕用於在imageview中加載圖像。 當用戶點擊第一個按鈕10時,圖片加載時,當用戶點擊第二個按鈕加載更多圖片10時,其他10張圖片不顯示,但是當他點擊第三個按鈕加載其他10張圖片時,得到java。 lang.OutOfMemoryError。我怎樣才能擺脫這個問題?我在這裏閱讀了很多帖子,並且我嘗試了各種方法,比如使用位圖,然後回收位圖,但是我給出了所有相同的錯誤。我不希望圖片在用戶點擊按鈕時被刪除,因爲他們必須多次重複使用。有什麼辦法可以推開內存中的圖像嗎?或重置內存緩存?提前幫助我感謝。這個代碼在Android的版本2.3.3,但在版本4.1崩潰 這是代碼:java.lang.OutOfMemoryError setBackgroundResource
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);
}
});
檢查此http://developer.android.com/training/displaying-bitmaps/manage-memory.html和這個http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Raghunandan
謝謝,但是當不再需要顯示資源時使用回收方法,我希望在以後的時間 – Gioele
可以重複使用,回收位圖時會出現什麼問題? – Raghunandan