我一直收到以下错误:
java.lang.OutOfMemoryError: Failed to allocate a 9961068 byte allocation with 4194304 free bytes and 5MB until OOM
在我的应用程序中,我有2个不同的广告单元,用于2种不同的活动。每个活动都通过一个按钮相互链接。但是,当我进入第3个活动时,弹出上面显示的错误并关闭app力。在我添加广告之前,这并没有发生。
当它试图显示缩放的位图但位图仅为488x567px时,似乎内存不足。我使用以下内容显示myPlayer是imageView的图像:
myPlayer.setImageResource(playerTypes[3]);
我可以做些什么来减少应用程序需要的内存?
以下是显示的第一个Activity的片段:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
}
});
以下是显示的下一个活动的片段:
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
MobileAds.initialize(getApplicationContext(), "<ad-unit-id>");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
begin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(players.getText().toString().equals("") || getTotalPlayers() == 0){
Toast.makeText(MainActivity.this, "How many players?", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(getBaseContext(), PlayerAssign.class);
intent.putExtra("PLAYER_COUNT", "" + getTotalPlayers());
startActivity(intent);
}
}
});
除了在xml文件中设置的背景图像之外,上述两个活动都没有任何图像。
下一个活动是发生错误的地方。
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!myPlayer.getTag().equals(R.drawable.contenthidden)){
Toast.makeText(PlayerAssign.this, "Press Next Player", Toast.LENGTH_SHORT).show();
} else if(numClicked <= numPlayers){
numClicked++;
int rand = (int) (Math.random()*100);
Log.d("rand",""+rand);
if(rand<25 && numMafia != numMafiaAdded){
myPlayer.setImageResource(playerTypes[0]);
myPlayer.setTag(playerTypes[0]);
numMafiaAdded++;
}else if (rand<50 && numDoctor != numDoctorAdded){
myPlayer.setImageResource(playerTypes[1]);
myPlayer.setTag(playerTypes[1]);
numDoctorAdded++;
} else if (rand<75 && numDetective !=numDetectiveAdded){
myPlayer.setImageResource(playerTypes[2]);
myPlayer.setTag(playerTypes[2]);
numDetectiveAdded++;
}else if (rand<100 && numVillager != numVillagerAdded){
myPlayer.setImageResource(playerTypes[3]);
myPlayer.setTag(playerTypes[3]);
numVillagerAdded++;
}
} else {
myPlayer.setImageResource(R.drawable.setupcomplete);
myPlayer.setTag(R.drawable.setupcomplete);
}
}
});