3

我使用 JPG 背景作为我活动的根视图,并将其放在drawable文件夹中。一切都很好,直到我决定为纵向和横向使用 2 种不同的壁纸。所以,我把第二张壁纸放在drawable-land文件夹里。

现在的问题是:当我在真实设备上测试应用程序时,每次我改变方向两次(从纵向到横向,然后从横向到纵向),应用程序崩溃并且我看到一个 loooooong 错误日志,关于OutOfMemoryException

09-08 14:13:21.567: E/dalvikvm-heap(16536): Out of memory on a 16384016-byte allocation.
09-08 14:13:21.567: I/dalvikvm(16536): "main" prio=5 tid=1 RUNNABLE
09-08 14:13:21.567: I/dalvikvm(16536):   | group="main" sCount=0 dsCount=0 obj=0x40ad2478 self=0x149a9d0
09-08 14:13:21.567: I/dalvikvm(16536):   | sysTid=16536 nice=0 sched=0/0 cgrp=default handle=1074488712
09-08 14:13:21.567: I/dalvikvm(16536):   | schedstat=( 0 0 0 ) utm=346 stm=52 core=1
09-08 14:13:21.567: I/dalvikvm(16536):   at android.graphics.Bitmap.nativeCreate(Native Method)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:530)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:505)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:357)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:777)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.content.res.Resources.loadDrawable(Resources.java:1940)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.view.View.<init>(View.java:2844)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.view.View.<init>(View.java:2781)
09-08 14:13:21.567: I/dalvikvm(16536):   at android.view.ViewGroup.<init>(ViewGroup.java:412)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.widget.RelativeLayout.<init>(RelativeLayout.java:179)
09-08 14:13:21.577: I/dalvikvm(16536):   at java.lang.reflect.Constructor.constructNative(Native Method)
09-08 14:13:21.577: I/dalvikvm(16536):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.view.LayoutInflater.createView(LayoutInflater.java:586)
09-08 14:13:21.577: I/dalvikvm(16536):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-08 14:13:21.577: I/dalvikvm(16536):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.app.Activity.setContentView(Activity.java:1835)
09-08 14:13:21.577: I/dalvikvm(16536):   at com.test.IndexActivity.onCreate(IndexActivity.java:58)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.app.Activity.performCreate(Activity.java:4465)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3371)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.app.ActivityThread.access$700(ActivityThread.java:127)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.os.Looper.loop(Looper.java:137)
09-08 14:13:21.577: I/dalvikvm(16536):   at android.app.ActivityThread.main(ActivityThread.java:4441)
09-08 14:13:21.577: I/dalvikvm(16536):   at java.lang.reflect.Method.invokeNative(Native Method)
09-08 14:13:21.577: I/dalvikvm(16536):   at java.lang.reflect.Method.invoke(Method.java:511)
09-08 14:13:21.577: I/dalvikvm(16536):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
09-08 14:13:21.577: I/dalvikvm(16536):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
09-08 14:13:21.577: I/dalvikvm(16536):   at dalvik.system.NativeStart.main(Native Method)
09-08 14:13:21.577: D/AndroidRuntime(16536): Shutting down VM
09-08 14:13:21.577: W/dalvikvm(16536): threadid=1: thread exiting with uncaught exception (group=0x40ad1210)
09-08 14:13:21.597: E/AndroidRuntime(16536): FATAL EXCEPTION: main
09-08 14:13:21.597: E/AndroidRuntime(16536): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.IndexActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3371)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.ActivityThread.access$700(ActivityThread.java:127)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.os.Looper.loop(Looper.java:137)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.ActivityThread.main(ActivityThread.java:4441)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at java.lang.reflect.Method.invokeNative(Native Method)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at java.lang.reflect.Method.invoke(Method.java:511)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at dalvik.system.NativeStart.main(Native Method)
09-08 14:13:21.597: E/AndroidRuntime(16536): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.Activity.setContentView(Activity.java:1835)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at com.rahpou.android.parnian.IndexActivity.onCreate(IndexActivity.java:58)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.Activity.performCreate(Activity.java:4465)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
09-08 14:13:21.597: E/AndroidRuntime(16536):    ... 12 more
09-08 14:13:21.597: E/AndroidRuntime(16536): Caused by: java.lang.reflect.InvocationTargetException
09-08 14:13:21.597: E/AndroidRuntime(16536):    at java.lang.reflect.Constructor.constructNative(Native Method)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
09-08 14:13:21.597: E/AndroidRuntime(16536):    ... 24 more
09-08 14:13:21.597: E/AndroidRuntime(16536): Caused by: java.lang.OutOfMemoryError
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.graphics.Bitmap.nativeCreate(Native Method)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:530)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:505)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:357)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:777)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.content.res.Resources.loadDrawable(Resources.java:1940)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.View.<init>(View.java:2844)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.View.<init>(View.java:2781)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.view.ViewGroup.<init>(ViewGroup.java:412)
09-08 14:13:21.597: E/AndroidRuntime(16536):    at android.widget.RelativeLayout.<init>(RelativeLayout.java:179)
09-08 14:13:21.597: E/AndroidRuntime(16536):    ... 27 more

似乎加载另一个可绘制对象的成本太高了,但我已经看到许多应用程序使用大而多样的图形和壁纸并且没有问题。那么,如何防止这种内存泄漏并帮助 android 回收/销毁以前的背景可绘制对象以避免 OutOfMemory?!

如果重要的话,我的手机屏幕和壁纸都有高清(720p)分辨率,两个壁纸(港口和陆地)的大小约为 100 KB。

编辑:我尝试了第一个答案并在我的代码中对其进行了修改,因为我有一个ScrollView作为根视图并且壁纸设置为其背景可绘制。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);            

    ScrollView root = (ScrollView)findViewById(R.id.root);
    // this line causes NullPointerException:
    BitmapDrawable drawable = (BitmapDrawable) root.getBackground();
    Bitmap bitmap = drawable.getBitmap();
    if (bitmap != null)
    {
    bitmap.recycle();
    }
}   

但事情变得最糟糕,我NullPointerException在代码中指定的行上进行了第一次方向更改。

另一件事是为什么需要分配 16 MB 内存来创建高清位图?!我认为 1280 像素 x 800 像素 x 8(字节/像素)= 8 MB 那么为什么需要 16 MB?!(我指的是 LogCat 的第一行,它说“在 16384016 字节分配上内存不足。”和 16384016 字节 ~ 16 MB。

4

5 回答 5

0

只需将其添加到您的mainfest.xml

如果您的 android:targetSdkVersion="12" 或更少

    android:configChanges="orientation|keyboardHidden">

如果你的 android:targetSdkVersion="13" 或更多

    android:configChanges="orientation|keyboardHidden|screensize">
于 2012-09-08T11:48:44.133 回答
0

的答案configChanges似乎暂时缓解了这个问题,但如果我们看一下 LogCat,我们会发现他有一个OutOfMemoryException.

这是在没有足够的内存分配时加载大位图的结果。

我建议您查看有关如何修复的 Android 文档:http: //developer.android.com/training/displaying-bitmaps/index.html

于 2012-09-08T13:26:57.150 回答
0

我想您已经解决了这个问题,但是由于您的回答,这是我找到的解决方案。

我为布局使用了两个不同的 ID,以及以下 OnConfigurationChanged 函数:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    BitmapDrawable drawable = null;
    if (findViewById(R.id.root_land) != null) {
        drawable = (BitmapDrawable) findViewById(R.id.root_land).getBackground();
    }
    if (findViewById(R.id.root_port) != null) {
        drawable = (BitmapDrawable) findViewById(R.id.root_port).getBackground();   

    }
    if (drawable != null){
        Bitmap bitmap = drawable.getBitmap();
        if (bitmap != null) {
            bitmap.recycle();
        }
    }
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.activity_main);
}

在清单(进入活动)中,我把这个:

android:configChanges="orientation|keyboardHidden|screensize">
于 2014-02-14T12:51:21.933 回答
0

使用以下代码。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);            

    myImage = findViewById(R.id.myImage);
    BitmapDrawable drawable = (BitmapDrawable) myImage.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    if (bitmap != null)
    {
        bitmap.recycle();
    }       
}

并在清单中:

<activity
  ...
    android:configChanges="keyboardHidden|orientation"
  ...
>
</activity>
于 2012-09-08T11:32:03.110 回答
0

System.gc()public void onConfigurationChanged方法中使用过:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    System.gc();
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.activity_main);
}
于 2016-06-22T13:57:29.647 回答