在我的活动中有很多图像,一段时间后应用程序崩溃(内存不足错误)。所以我想清除图像,如果我关闭活动,但它没有工作。我的代码:
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
public class Jubel extends AppCompatActivity {
public Spinner spinner1;
public String[] spinnertext = {"Laufbewegungen", "Abschlussbewegungen (1/2)", "Abschlussbewegungen (2/2)", "EAS FC - Freischaltbare Objekte"};
public ImageView imageView12;
public ImageView imageView8;
public ImageView imageView16;
public ImageView imageView17;
public ImageView imageView18;
public ImageView imageView20;
public ImageView imageView21;
public ImageView imageView15;
public ImageView imageView13;
public ImageView imageView22;
public ImageView imageView19;
public ImageView imageView23;
public ImageView imageView24;
public ImageView imageView25;
public ImageView imageView26;
public ImageView imageView27;
public ImageView imageView29;
public ImageView imageView28;
public ImageView imageView30;
public ImageView imageView31;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jubel);
imageView12 = (ImageView) findViewById(R.id.imageView12);
imageView12.setImageResource(R.drawable.thumbsuck2);
imageView8 = (ImageView) findViewById(R.id.imageView8);
imageView8.setImageResource(R.drawable.onearmraised1);
imageView16 = (ImageView) findViewById(R.id.imageView16);
imageView16.setImageResource(R.drawable.wristflick6);
imageView17 = (ImageView) findViewById(R.id.imageView17);
imageView17.setImageResource(R.drawable.aeroplane7);
imageView18 = (ImageView) findViewById(R.id.imageView18);
imageView18.setImageResource(R.drawable.pointtosky8);
imageView20 = (ImageView) findViewById(R.id.imageView20);
imageView20.setImageResource(R.drawable.canyouhearme11);
imageView21 = (ImageView) findViewById(R.id.imageView21);
imageView21.setImageResource(R.drawable.handsout12);
imageView15 = (ImageView) findViewById(R.id.imageView15);
imageView15.setImageResource(R.drawable.armsout5);
imageView13 = (ImageView) findViewById(R.id.imageView13);
imageView13.setImageResource(R.drawable.eartwist3);
imageView22 = (ImageView) findViewById(R.id.imageView22);
imageView22.setImageResource(R.drawable.fingerpoint4);
imageView19 = (ImageView) findViewById(R.id.imageView19);
imageView19.setImageResource(R.drawable.telephone10);
imageView23 = (ImageView) findViewById(R.id.imageView23);
imageView23.setImageResource(R.drawable.shhhhh9);
imageView24 = (ImageView) findViewById(R.id.imageView24);
imageView24.setImageResource(R.drawable.comeon13);
imageView25 = (ImageView) findViewById(R.id.imageView25);
imageView25.setImageResource(R.drawable.doublearmswing15);
imageView26 = (ImageView) findViewById(R.id.imageView26);
imageView26.setImageResource(R.drawable.flyingbird16);
imageView27 = (ImageView) findViewById(R.id.imageView27);
imageView27.setImageResource(R.drawable.handonhead17);
imageView29 = (ImageView) findViewById(R.id.imageView29);
imageView29.setImageResource(R.drawable.armspointingup19);
imageView28 = (ImageView) findViewById(R.id.imageView28);
imageView28.setImageResource(R.drawable.heartsymbol18);
imageView30 = (ImageView) findViewById(R.id.imageView30);
imageView30.setImageResource(R.drawable.windmill20);
imageView31 = (ImageView) findViewById(R.id.imageView31);
imageView31.setImageResource(R.drawable.blowkisses14);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> spinneradapter = new ArrayAdapter<String>(Jubel.this, android.R.layout.simple_spinner_dropdown_item, spinnertext);
spinner1.setAdapter(spinneradapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinnerposition = spinner1.getSelectedItemPosition();
switch (spinnerposition) {
case 0: {
break;
}
case 1: {
finish();
Intent intent = new Intent(Jubel.this,JubelAbschluss.class);
startActivity(intent);
break;
}
case 2: {
finish();
Intent intent = new Intent(Jubel.this,JubelAbschluss2.class);
startActivity(intent);
break;
}
case 3: {
finish();
Intent intent = new Intent(Jubel.this,JubelEAS.class);
startActivity(intent);
break;
}
}
}
@Override
public void onNothingSelected (AdapterView < ? > parent){
}
} );}
public void ToHome(View view) {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}
@Override
public void onStop() {
((BitmapDrawable)imageView12.getDrawable()).getBitmap().recycle();
imageView12.setImageDrawable(null);
((BitmapDrawable)imageView8.getDrawable()).getBitmap().recycle();
imageView8.setImageDrawable(null);
((BitmapDrawable)imageView16.getDrawable()).getBitmap().recycle();
imageView16.setImageDrawable(null);
((BitmapDrawable)imageView17.getDrawable()).getBitmap().recycle();
imageView17.setImageDrawable(null);
((BitmapDrawable)imageView18.getDrawable()).getBitmap().recycle();
imageView18.setImageDrawable(null);
((BitmapDrawable)imageView20.getDrawable()).getBitmap().recycle();
imageView20.setImageDrawable(null);
((BitmapDrawable)imageView21.getDrawable()).getBitmap().recycle();
imageView21.setImageDrawable(null);
((BitmapDrawable)imageView15.getDrawable()).getBitmap().recycle();
imageView15.setImageDrawable(null);
((BitmapDrawable)imageView13.getDrawable()).getBitmap().recycle();
imageView13.setImageDrawable(null);
((BitmapDrawable)imageView22.getDrawable()).getBitmap().recycle();
imageView22.setImageDrawable(null);
((BitmapDrawable)imageView19.getDrawable()).getBitmap().recycle();
imageView19.setImageDrawable(null);
((BitmapDrawable)imageView23.getDrawable()).getBitmap().recycle();
imageView23.setImageDrawable(null);
((BitmapDrawable)imageView24.getDrawable()).getBitmap().recycle();
imageView24.setImageDrawable(null);
((BitmapDrawable)imageView25.getDrawable()).getBitmap().recycle();
imageView25.setImageDrawable(null);
((BitmapDrawable)imageView26.getDrawable()).getBitmap().recycle();
imageView26.setImageDrawable(null);
((BitmapDrawable)imageView27.getDrawable()).getBitmap().recycle();
imageView27.setImageDrawable(null);
((BitmapDrawable)imageView28.getDrawable()).getBitmap().recycle();
imageView28.setImageDrawable(null);
((BitmapDrawable)imageView29.getDrawable()).getBitmap().recycle();
imageView29.setImageDrawable(null);
((BitmapDrawable)imageView30.getDrawable()).getBitmap().recycle();
imageView30.setImageDrawable(null);
((BitmapDrawable)imageView31.getDrawable()).getBitmap().recycle();
imageView31.setImageDrawable(null);
super.onStop();
}
}
答案 0 :(得分:0)
您可能想尝试使用Glide。
它将在很大程度上为您处理回收位图,它可以解决您在制作带图像的应用时遇到的大量内存问题。
但是,您的布局设计很可能是主要原因,您可能需要考虑一次在屏幕上显示较少的图像,或使用较低分辨率的图像。
希望这有帮助。
答案 1 :(得分:0)
在您的代码中尝试更改
@覆盖 public void onStop(){ ..............
....................................... .................................. }
到
@覆盖
public void onFinish(){ .......................... }