当我在Android应用程序中进行第三次活动时,我的应用程序中出现了java.lang.OutOfMemoryError。我使用Android监视器检查我的应用程序运行期间的内存使用情况,从第一个屏幕开始,它使用的是200mb而不是很多。第二个活动使用了大约250个。我将进入我的第三个活动,然后单击应用程序屏幕上的按钮会崩溃。
我删除了我的三个活动的背景图片,应用程序运行了更长的时间。但每次我在第三个活动和第二个活动之间切换(使用意图)时,分配的内存会不断增加,直到应用程序最终崩溃(只是在活动之间切换)。
这是否能为您提供关于我的应用程序发生了什么的线索?
这是我的第一个活动及其xml文件的一些代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.norsemeadgames.tobiasandthegiant.MainActivity"
tools:showIn="@layout/activity_main"
android:background="@drawable/menu_back">
<Button
android:layout_width="90dp"
android:layout_height="30dp"
android:text="Start"
android:id="@+id/buttonStart"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="133dp"
android:textColor="#ffffff"
android:background="@drawable/menu_items" />
<Button
android:layout_width="90dp"
android:layout_height="30dp"
android:text="Resume"
android:id="@+id/buttonResume"
android:layout_marginTop="20dp"
android:textColor="#ffffff"
android:background="@drawable/menu_items"
android:layout_below="@+id/buttonStart"
android:layout_centerHorizontal="true" />
</RelativeLayout>
现在主要活动java文件
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button buttonStart;
Button buttonResume;
Button buttonQuit;
Animation wobble;
Player player;
Monster bill = new Monster(Monster.randMonsterName(), 10, 200);
@Override
protected void onCreate(Bundle savedInstanceState) {
//makes activity run in fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wobble = AnimationUtils.loadAnimation(this, R.anim.wobble);
buttonStart = (Button)findViewById(R.id.buttonStart);
buttonResume = (Button)findViewById(R.id.buttonResume);
buttonStart.setOnClickListener(this);
buttonResume.setOnClickListener(this);
}
@Override
public void onResume(){
super.onResume();
player = new Player();
}
@Override
public void onClick (View v) {
switch (v.getId()){
case R.id.buttonStart:
buttonStart.startAnimation(wobble);
wobble.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent(MainActivity.this, CityRendActivity.class);
intent.putExtra("player", player);
startActivity(intent);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
}
}
}
这里是City Rend java文件
public class CityRendActivity extends AppCompatActivity implements View.OnClickListener {
int storyPage = 1;
TextView storyWindow;
TextView stats;
Player player;
ImageButton buttonMonsters;
@Override
protected void onCreate(Bundle savedInstanceState) {
//makes activity run in fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city_rend);
storyWindow = (TextView)findViewById(R.id.textStoryWindow);
storyWindow.setText(GameElements.fillStoryWindow(1));
storyWindow.setOnClickListener(this);
stats =(TextView)findViewById(R.id.textStats);
buttonMonsters = (ImageButton)findViewById(R.id.buttonMonsters);
buttonMonsters.setOnClickListener(this);
}
@Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
Player player = (Player)intent.getSerializableExtra("player");
stats.setText("HP = " + player.getHealth() + " | Str = " + player.getStrength() + " | Gold = " +
player.getGP() + " | Wpn = " + player.getWeapon());
this.player = player;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.textStoryWindow:
if(storyPage == 0){
storyPage = 1;
}
if(storyPage == 1){
storyPage = 2;
}else if(storyPage == 2){
storyPage = 3;
}else if(storyPage == 3) {
storyPage = 1;
}
storyWindow.setText(GameElements.fillStoryWindow(storyPage));
break;
case R.id.buttonMonsters:
Intent intent = new Intent(CityRendActivity.this, MonstersActivity.class);
intent.putExtra("player", player);
startActivity(intent);
}
}
}
现在怪物活动java文件
public class MonstersActivity extends AppCompatActivity implements View.OnClickListener {
Player player;
Button buttonBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
//makes activity run in fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monsters);
buttonBack = (Button)findViewById(R.id.buttonBack);
buttonBack.setOnClickListener(this);
}
@Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
Player intentPlayer = (Player)intent.getSerializableExtra("player");
player = intentPlayer;
player.setHealth(10);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(MonstersActivity.this, CityRendActivity.class);
intent.putExtra("player", player);
startActivity(intent);
}
}