1

我一直在为客户开发这个应用程序。我们已经准备好发布它,但在测试中我意识到它几乎在任何时候都会崩溃 onPause(), onResume() 被调用。特别是获取文本或旋转屏幕。或者在某些情况下只是在屏幕之间切换。每次它给我一个内存不足的错误。所以很明显,背景、按钮等的图像是罪魁祸首,因为它们是唯一使用任何实际内存量的元素。所以我把它精简为一个完全平淡无奇的应用程序,只有 2 个按钮,上面有图像。

这些按钮是 60k PNG,已通过 Photoshop 针对移动设备进行了优化。

我做了很多研究,并花时间在 freenode 上的#android-dev 上。我没有看到任何可以解决此问题的方法。

我根据android dev上的文档处理背景。这部分不在此代码中,否则一旦我可以让它与带有 2 个按钮的空白 ap 一起工作,它就会被立即添加到 onCreate() 方法中。(有适当的观点)

目前不在应用程序中,但将被添加回来

private void setNewPoolBackground() {
    setContentView(<FOO>);
    Bitmap image = getImage(R.drawable.background);
    Drawable d = new BitmapDrawable(image);
    rLayout = (RelativeLayout) findViewById (<FOO>);
    rLayout.setBackgroundDrawable(d);
}
private Bitmap getImage(int img) {
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    // The new size we want to scale to
    final int REQUIRED_SIZE = 70;
    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }
    Bitmap image = BitmapFactory.decodeStream(this.getResources()
            .openRawResource(img), null, o2);
    return image;
}

代码:TestRotation.java

package test.t;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class TestRotateActivity extends Activity {

    Bitmap image;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //setNewPoolBackground();
    }


    @Override
    protected void onPause() {
        super.onPause();
        // saveState();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        //View v 
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // TODO Auto-generated method stub

    }

}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/group_edit"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5pt"
        android:layout_marginLeft="8pt"
        android:layout_marginRight="7pt"
        android:layout_marginTop="50pt"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" 
        android:isScrollContainer="false"
        >

        <LinearLayout
            android:id="@+id/linlayGN"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >






            <TextView
                android:id="@+id/tvGroupTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="group_title" />

            <EditText
                android:id="@+id/group_title"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <requestFocus />
        </LinearLayout>

        <ScrollView
            android:id="@+id/ScrollView01"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:isScrollContainer="false"
         >


            <LinearLayout
                android:id="@+id/newUserList"
                android:layout_width="233dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/group_title"
                android:layout_weight="2.44"
                android:orientation="vertical" >

            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    <Button
        android:id="@+id/addgbutton"
        android:layout_width="40pt"
        android:layout_height="25pt"
        android:layout_alignLeft="@+id/moremembutton"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="28dp"
        android:layout_marginLeft="64dp"
        android:background="@drawable/finish"
        android:onClick="myClickHandler" >
    </Button>

    <Button
        android:id="@+id/moremembutton"
        android:layout_width="40pt"
        android:layout_height="25pt"
        android:layout_alignBottom="@+id/addgbutton"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="22dp"
        android:layout_marginLeft="81dp"
        android:background="@drawable/addmember"
        android:onClick="myClickHandler" >
    </Button>

</RelativeLayout>

日志

09-19 03:17:57.546: D/dalvikvm(278): GC_EXTERNAL_ALLOC freed 806 objects / 56864 bytes in 45ms
09-19 03:17:57.876: D/dalvikvm(278): GC_EXTERNAL_ALLOC freed 206 objects / 9736 bytes in 37ms
09-19 03:19:00.135: D/dalvikvm(278): GC_EXTERNAL_ALLOC freed 707 objects / 25800 bytes in 94ms
09-19 03:19:00.545: D/dalvikvm(278): GC_EXTERNAL_ALLOC freed 28 objects / 1624 bytes in 33ms
09-19 03:19:06.885: D/dalvikvm(278): GC_EXTERNAL_ALLOC freed 597 objects / 24008 bytes in 37ms
09-19 03:19:07.605: D/dalvikvm(278): GC_EXTERNAL_ALLOC freed 523 objects / 31384 bytes in 32ms
09-19 03:19:07.916: D/dalvikvm(278): GC_EXTERNAL_ALLOC freed 84 objects / 3816 bytes in 32ms
09-19 03:19:19.346: E/dalvikvm-heap(278): 3122676-byte external allocation too large for this process.
09-19 03:19:19.355: E/GraphicsJNI(278): VM won't let us allocate 3122676 bytes
09-19 03:19:19.375: D/AndroidRuntime(278): Shutting down VM
09-19 03:19:19.375: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-19 03:19:19.455: E/AndroidRuntime(278): FATAL EXCEPTION: main
09-19 03:19:19.455: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.t/test.t.TestRotateActivity}: android.view.InflateException: Binary XML file line #70: Error inflating class <unknown>
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2400(ActivityThread.java:125)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-19 03:19:19.455: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
09-19 03:19:19.455: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
09-19 03:19:19.455: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-19 03:19:19.455: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-19 03:19:19.455: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
09-19 03:19:19.455: E/AndroidRuntime(278): Caused by: android.view.InflateException: Binary XML file line #70: Error inflating class <unknown>
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.view.LayoutInflater.createView(LayoutInflater.java:513)
09-19 03:19:19.455: E/AndroidRuntime(278):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-19 03:19:19.455: E/AndroidRuntime(278):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.app.Activity.setContentView(Activity.java:1647)
09-19 03:19:19.455: E/AndroidRuntime(278):  at test.t.TestRotateActivity.onCreate(TestRotateActivity.java:21)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-19 03:19:19.455: E/AndroidRuntime(278):  ... 12 more
09-19 03:19:19.455: E/AndroidRuntime(278): Caused by: java.lang.reflect.InvocationTargetException
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.widget.Button.<init>(Button.java:65)
09-19 03:19:19.455: E/AndroidRuntime(278):  at java.lang.reflect.Constructor.constructNative(Native Method)
09-19 03:19:19.455: E/AndroidRuntime(278):  at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.view.LayoutInflater.createView(LayoutInflater.java:500)
09-19 03:19:19.455: E/AndroidRuntime(278):  ... 23 more
09-19 03:19:19.455: E/AndroidRuntime(278): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.graphics.Bitmap.nativeCreate(Native Method)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.view.View.<init>(View.java:1885)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.widget.TextView.<init>(TextView.java:327)
09-19 03:19:19.455: E/AndroidRuntime(278):  at android.widget.Button.<init>(Button.java:69)
09-19 03:19:19.455: E/AndroidRuntime(278):  ... 27 more
4

2 回答 2

3

如果我们正在处理大量位图和大图像,OOM 错误在 android 中并不是一个意外的异常。由于 android 手机和选项卡有许多堆大小,因此很难使其与所有设备兼容。大多数 android 设备的低堆内存。通过代码我们可以分配它,因为我们只有有限的访问权限来清除内存。在每个进程调用System.gc()并使用垃圾收集器分配未使用的内存。仍然不确定所有未使用的内存都被清除了。之后回收所有位图你的使用bitmap.recycle()。也不要在应用程序中使用很多位图。构建一个位图数量非常少的应用程序,当你使用它时回收它并一次又一次地使用它。离开活动时,您可以从视图中取消绑定所有可绘制对象。

private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
            try {
                view.getBackground().setCallback(null);
                ((BitmapDrawable) view.getBackground()).getBitmap().recycle();
                view.destroyDrawingCache();
                view.notifyAll();
            } catch (Exception e) {
            }

        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            ((ViewGroup) view).removeAllViews();
        }
    }

您还可以通过代码释放所有未使用的内存..

public void clearAllocatedMemory() {
        try {
            List.removeAllViews();
            adapter.releaseAllMemory();
        } catch (Exception e) {
        }
        adapter = null;
    }

我认为做所有这些都会最大限度地分配你的内存,你可以减少这个异常的机会。

于 2012-09-19T04:22:32.887 回答
2
   if your android:targetSdkVersion="12" or less
   add this in you each Activitiy declaration in manitest.xml
      android:configChanges="orientation|keyboardHidden">

      if your  android:targetSdkVersion="13" or more
     add this in you each Activitiy declaration in manitest.xml
       android:configChanges="orientation|keyboardHidden|screenSize">
于 2012-09-19T04:08:55.587 回答