【发布时间】:2015-11-09 15:45:30
【问题描述】:
我认为我没有遵循优化 android 应用程序内存的标准一般规则,并且我不断收到 OutOfMemoryError,现在我正在尝试在 Android Studio 中使用 DDMS 调试和查找源,但我不知道如何阅读它以及如何找到占用最大空间的对象。以下是我的代码。
选择级别活动
public class ChooseLevelActivity extends Activity implements View.OnClickListener {
ImageView level001, level002, level003, level004, level005;
int last_level, best_move;
String best_time, level_selected;
TextView levelName, bestMove, bestTime, no_level_finished;
Class <? extends Activity> classVariable;
Button playBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_level);
level001 = (ImageView) findViewById(R.id.level001);
level002 = (ImageView) findViewById(R.id.level002);
level003 = (ImageView) findViewById(R.id.level003);
level004 = (ImageView) findViewById(R.id.level004);
level005 = (ImageView) findViewById(R.id.level005);
levelName = (TextView) findViewById(R.id.level_name);
bestMove = (TextView) findViewById(R.id.best_move);
bestTime = (TextView) findViewById(R.id.best_time);
no_level_finished = (TextView) findViewById(R.id.no_level_finished);
playBtn = (Button) findViewById(R.id.playBtn);
// First check if file exists
File recordFile = getBaseContext().getFileStreamPath("levels.json");
if (recordFile.exists()) {
// Get the JSON Object from the data
JSONObject parent = parseJSONData("levels.json");
// Array of ImageView
final ImageView[] levelsArray = {level001, level002, level003, level004, level005};
// This will store all the values inside "best_move and time" in an element string
try {
last_level = parent.getInt("level");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i = 0; i < last_level; i++) {
levelsArray[i].setVisibility(View.VISIBLE);
levelsArray[i].setOnClickListener(this);
}
} else {
no_level_finished.setVisibility(View.VISIBLE);
playBtn.setVisibility(View.GONE);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.level001:
level_selected = "level001";
classVariable = Level001Activity.class;
break;
case R.id.level002:
level_selected = "level002";
classVariable = Level002Activity.class;
break;
case R.id.level003:
level_selected = "level003";
classVariable = Level003Activity.class;
break;
case R.id.level004:
level_selected = "level004";
classVariable = Level004Activity.class;
break;
case R.id.level005:
level_selected = "level005";
classVariable = Level005Activity.class;
break;
}
String fileName = "record_" + level_selected + ".json";
// Get the JSON Object from the data
JSONObject parents = parseJSONData(fileName);
// This will store all the values inside "best_move and time" in an element string
try {
best_move = parents.getInt("best_move");
best_time = parents.getString("best_time");
} catch (JSONException e) {
// e.printStackTrace();
}
levelName.setText("Level " + level_selected.substring(5));
bestMove.setText("Best Move: " + best_move);
bestTime.setText("Best Time: " + best_time);
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
classVariable);
startActivity(i);
finish();
}
});
}
public JSONObject parseJSONData(String file) {
String JSONString = null;
JSONObject jsonObject = null;
try {
// Open the inputStream to the file
FileInputStream fin = openFileInput(file);
int sizeOfJSONFile = fin.available();
// array that will store all the data
byte[] bytes = new byte[sizeOfJSONFile];
// reading data into the array from the file
fin.read(bytes);
// close the input stream
fin.close();
JSONString = new String(bytes, "UTF-8");
jsonObject = new JSONObject(JSONString);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JSONException x) {
x.printStackTrace();
return null;
}
return jsonObject;
}
}
choose_level.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="You have not finish any level yet."
android:id="@+id/no_level_finished"
android:visibility="gone"
/>
<RelativeLayout
android:id="@+id/level_completed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp">
<ImageView
android:id="@+id/level001"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/smiley"
android:visibility="gone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<ImageView
android:id="@+id/level002"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/heaven_full"
android:layout_toRightOf="@+id/level001"
android:visibility="gone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<ImageView
android:id="@+id/level003"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/smiley"
android:layout_toRightOf="@+id/level002"
android:visibility="gone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<ImageView
android:id="@+id/level004"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_toRightOf="@+id/level003"
android:visibility="gone"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
/>
<ImageView
android:id="@+id/level005"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="@+id/level004"
android:visibility="gone"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/level_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/level_completed"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
>
<TextView
android:id="@+id/level_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/best_move"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/level_name"/>
<TextView
android:id="@+id/best_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/best_move"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/playBtn"
android:layout_below="@+id/best_time"
android:text="Play Level"/>
</RelativeLayout>
</RelativeLayout>
DDMS 堆选项卡
如何阅读 DDMS 并利用它来找出占用如此多内存的问题?请让我知道是否需要其他任何东西来调试它,例如分配跟踪器或 DDMS 中的线程选项卡。谢谢大家的帮助,真的很感激。
【问题讨论】:
-
是的,这是关于如何使用它的手册,但我在尝试将它连接到我的项目时感到困惑,所以如果有人有更好的方法来查找故障,那将是一个很大的帮助。还是谢谢你,一会儿看youtube。
标签: java android android-studio memory-leaks ddms