我释放了每个对象,在onDestroy()方法中回收所有位图:
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++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
unbindDrawables(findViewById(R.id.RootView));
bitmap.recycle();
bitmap=null;
System.gc();
}
我已经在每个类onDestroy()方法中完成了这个编码。但我的logcat仍然向我显示错误:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
android.app.ActivityThread.access$600(ActivityThread.java:123)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4424)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
android.view.LayoutInflater.createView(LayoutInflater.java:606)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
android.view.LayoutInflater.inflate(LayoutInflater.java:466)
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
android.view.LayoutInflater.inflate(LayoutInflater.java:352)
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
android.app.Activity.setContentView(Activity.java:1835)
android.app.Activity.performCreate(Activity.java:4465)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
java.lang.reflect.Constructor.constructNative(Native Method)
java.lang.reflect.Constructor.newInstance(Constructor.java:417)
android.view.LayoutInflater.createView(LayoutInflater.java:586)
... 23 more
Caused by: java.lang.OutOfMemoryError
android.graphics.Bitmap.nativeCreate(Native Method)
android.graphics.Bitmap.createBitmap(Bitmap.java:605)
android.graphics.Bitmap.createBitmap(Bitmap.java:551)
android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
android.content.res.Resources.loadDrawable(Resources.java:1935)
android.content.res.TypedArray.getDrawable(TypedArray.java:601)
android.view.View.<init>(View.java:2785)
android.view.View.<init>(View.java:2722)
android.view.ViewGroup.<init>(ViewGroup.java:379)
android.widget.RelativeLayout.<init>(RelativeLayout.java:174)
... 26 more
答案 0 :(得分:8)
标题说明了一切 - 您正在尝试为一个图像一次分配 73兆字节的堆。仅此一项就超过了大多数现代设备上可用的堆大小 - 64兆字节。您需要找到一种方法来使用重要的小图像。