3

我释放了每个对象,回收了 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
4

1 回答 1

8

标题说明了一切——您试图一次为一张图像分配73 兆字节的堆。仅此一项就超过了大多数现代设备上可用的堆大小——64 兆字节。您需要找到一种方法来使用明显更小的图像。

于 2012-09-11T11:28:55.093 回答