我,我自己,对Android很新。我曾经做过一次音板,但现在我做的更好了。 我有4个主题(活动)。当我在它们之间切换一段时间时,它会给我一个OutOfMemoryError。我尝试降低背景的分辨率,图像按钮,它仍然让我错误。 按下按钮后,我立即杀死上一个活动。 我也使用了unbindDrawables,但这似乎也没有帮助。
非常感谢。
我的开始屏幕活动,包含4个指向活动的链接:
public class Startscreen extends Activity {
ImageButton imageButton1;
ImageButton imageButton2;
ImageButton imageButton3;
ImageButton imageButton4;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startscreen);
addListenerOnButton();
}
private void unbindDrawables(View LinearLayout) {
if (LinearLayout.getBackground() != null) {
LinearLayout.getBackground().setCallback(null);
}
if (LinearLayout instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) LinearLayout).getChildCount(); i++) {
unbindDrawables(((ViewGroup) LinearLayout).getChildAt(i));
}
((ViewGroup) LinearLayout).removeAllViews();
}
}
public void addListenerOnButton() {
imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Startscreen.this, Airlines.class);
startActivity(intent);
overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
unbindDrawables(findViewById(R.id.scrollView));
finish();
}
});
imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
imageButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Startscreen.this, Cabdriver.class);
startActivity(intent);
overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
unbindDrawables(findViewById(R.id.scrollView));
finish();
}
});
imageButton3 = (ImageButton) findViewById(R.id.imageButton4);
imageButton3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Startscreen.this, CakeSoup.class);
startActivity(intent);
overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
unbindDrawables(findViewById(R.id.scrollView));
finish();
}
});
imageButton4 = (ImageButton) findViewById(R.id.imageButton3);
imageButton4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Startscreen.this, BorisLife.class);
startActivity(intent);
overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
unbindDrawables(findViewById(R.id.scrollView));
finish();
}
});
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://www.youtube.com/channel/UCS5tt2z_DFvG7-39J3aE-bQ"));
startActivity(intent);
}
});
}
;
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
我的第一个活动,除声音的名称外,其中所有4个活动几乎完全相同。
public class Airlines extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton button1 = (ImageButton) this.findViewById(R.id.Button1);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.airplanetime);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp1.start();
}
});
ImageButton button2 = (ImageButton) this.findViewById(R.id.Button2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.masterofskies);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp2.start();
}
});
ImageButton button3 = (ImageButton) this.findViewById(R.id.Button3);
final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.oohh);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp3.start();
}
});
ImageButton button4 = (ImageButton) this.findViewById(R.id.Button4);
final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.friendyouasshole);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp4.start();
}
});
ImageButton button5 = (ImageButton) this.findViewById(R.id.Button5);
final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.fuckyou);
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp5.start();
}
});
ImageButton button6 = (ImageButton) this.findViewById(R.id.Button6);
final MediaPlayer mp6 = MediaPlayer.create(this, R.raw.hopeyouenjoy);
button6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp6.start();
}
});
ImageButton button7 = (ImageButton) this.findViewById(R.id.Button7);
final MediaPlayer mp7 = MediaPlayer.create(this, R.raw.oyblin);
button7.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp7.start();
}
});
ImageButton button8 = (ImageButton) this.findViewById(R.id.Button8);
final MediaPlayer mp8 = MediaPlayer.create(this, R.raw.whatthefuck);
button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp8.start();
}
});
ImageButton button9 = (ImageButton) this.findViewById(R.id.Button9);
final MediaPlayer mp9 = MediaPlayer.create(this, R.raw.muchbetter);
button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp9.start();
}
});
ImageButton button10 = (ImageButton) this.findViewById(R.id.Button10);
final MediaPlayer mp10 = MediaPlayer.create(this, R.raw.naturaltalent);
button10.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp10.start();
}
});
ImageButton button11 = (ImageButton) this.findViewById(R.id.Button11);
final MediaPlayer mp11 = MediaPlayer.create(this, R.raw.neewpff);
button11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp11.start();
}
});
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://www.youtube.com/channel/UCS5tt2z_DFvG7-39J3aE-bQ"));
startActivity(intent);
}
});
}
private void unbindDrawables(View LinearLayout) {
if (LinearLayout.getBackground() != null) {
LinearLayout.getBackground().setCallback(null);
}
if (LinearLayout instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) LinearLayout).getChildCount(); i++) {
unbindDrawables(((ViewGroup) LinearLayout).getChildAt(i));
}
((ViewGroup) LinearLayout).removeAllViews();
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
unbindDrawables(findViewById(R.id.linearLeiout));
Intent intent = new Intent(Airlines.this, LoadingScreenActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
this.finish();
}
}
答案 0 :(得分:0)
我可以在这里看到一些可能导致问题的潜在问题。
MediaPlayer.create()
的说明,请说:完成MediaPlayer后,您应该调用release()来释放资源。如果没有发布,太多的MediaPlayer实例将导致异常。
并且您似乎没有打电话给release()
。您可能因未释放MediaPlayer
而导致内存泄漏。
在Airlines
课程中,无论是否使用,您都会创建11个MediaPlayer
个对象。我不清楚你是否希望它能够同时播放多个声音。如果你这样做可能需要有多个MediaPlayer
,但你可以考虑在onClick()
方法中创建它们,这样只有在它们快速的情况下才需要创建它们足以在点击后加载声音。如果您不需要同时播放声音,那么单MediaPlayer
可能会更好。
在调用{{Context
时,使用getApplicationContext()
代替Activity
Context
,最好使用应用MediaPlayer.create()
1}}虽然如果您发布MediaPlayer
个对象,希望您无论如何都不会泄漏任何Activity
。