2
我刚开始使用Android。我尝试了一个方向改变的应用程序。android - OutofMemoryError - 位图大小超过虚拟机预算 - 在方向更改上
我面临的位图大小超过虚拟机预算。已经在stackoverflow中经历了很多帖子,无法找出问题所在。异常是抛出setContentView(R.layout.level1);在onCreate。当我改变方向时发生这种情况。
我试过所有论坛和计算器,但无法弄清楚。试图在过去3天内解决这个问题。
下面的类正在使用intent和startActivity按钮单击从另一个活动中调用。
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("onCreate", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.level1);
if(!isOrientationChanged) //this part is executed if orientation is not changed (activity starts as usual)
{
drawableList = new ArrayList<Drawable>();
drawableList.add(getResources().getDrawable(colorEnum[0]));
drawableList.add(getResources().getDrawable(colorEnum[1]));
}
isOrientationChanged = false;
timeView = (TextView) findViewById(R.id.timeView);
colorButton = (Button) findViewById(R.id.game_button);
}
@Override
protected void onResume() {
scoreView = (TextView) findViewById(R.id.scoreView);
scoreView.setText("Score: " + score);
hand.postDelayed(runThread, 0);
super.onResume();
}
@Override
public Object onRetainNonConfigurationInstance() {
isOrientationChanged = true;
return null; //as you are not returning an object you are not leaking memory here
}
@Override
protected void onPause() {
hand.removeCallbacks(runThread);
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindDrawables(findViewById(R.id.RootView));
System.gc();
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
Log.d("onDestroy","Inside loop to getting child "+((ViewGroup) view).getChildAt(i));
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
hand.removeCallbacks(runThread);
}
/** The run thread. */
Thread runThread = new Thread() {
@Override
public void run() {
timeView.setText("Time Left: " + timeLeftVal);
:
:
:
} else {
changeColor();
:
:
hand.postDelayed(runThread, GET_DATA_INTERVAL);
}
}
};
/**
* Change color.
*/
private void changeColor() {
colorButton.setBackgroundDrawable(drawableList.get(randomNum));
}
- 在onCreate方法,创造了绘制列表,并对其进行初始化上的第一负载。
- 使用线程随机设置按钮图像背景
- 调用unbindDrawables()方法onDestroy,以便在方向上更改旧视图将从内存中删除。
- hand.removeCallbacks(runThread)也被称为的onPause方法在onRetainNonConfigurationInstance返回null
- ()
我已经解决了这一问题。 小错误导致了这个大问题。我在处理程序中使用了Thread而不是Runnable。因此,removeCallback没有按预期工作。
什么是你的清单文件安装到做什么? ,您是否将方向更改传递给活动以进行手动处理,或者您要进行旧式并经常崩溃的摧毁和重建? – Chris
你正在加载的图像的大小是多少?你加载了多少图片?正如克里斯所说,在方向转变时,所有的活动都被剥离并重新加载。 问题可能是先前方向的图像仍然被加载,而下一个方向的图像正在加载。应用程序crah,因为WM内存不足。 调用** unbindDrawables()** ** System.gc()**不确保在加载下一张图像之前卸载图像。 – jobesu14
关于此的好文章:http://android-developers.blogspot.com/2009/02/faster-screen-orientation-change.html – MobileCushion