0

在我的应用程序中,我在 main.xml 文件中使用了大量布局和大量膨胀。当我将此 xml 设置为 contentview 应用程序崩溃时。在此之前我有两个启动画面。在那些闪屏中,我使用了两个 >200kb 大小的图像。从日志看来,可能是一些内存泄漏问题。这是我的日志:

09-06 03:26:14.749: E/GraphicsJNI(2212): VM won't let us allocate 2764800 bytes
09-06 03:26:14.788: D/AndroidRuntime(2212): Shutting down VM
09-06 03:26:14.788: W/dalvikvm(2212): threadid=1: thread exiting with uncaught exception (group=0x4001d7e0)
09-06 03:26:14.788: I/EXCEPOTION(2212): Aise
09-06 03:26:14.952: E/AndroidRuntime(2212): FATAL EXCEPTION: main
09-06 03:26:14.952: E/AndroidRuntime(2212): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jukte.forgivenessapplication/com.jukte.forgivenessapplication.Home}: android.view.InflateException: Binary XML file line #147: Error inflating class <unknown>
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.os.Looper.loop(Looper.java:123)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at java.lang.reflect.Method.invoke(Method.java:521)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at dalvik.system.NativeStart.main(Native Method)
09-06 03:26:14.952: E/AndroidRuntime(2212): Caused by: android.view.InflateException: Binary XML file line #147: Error inflating class <unknown>
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.app.Activity.setContentView(Activity.java:1647)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at com.jukte.forgivenessapplication.Home.onCreate(Home.java:142)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-06 03:26:14.952: E/AndroidRuntime(2212):     ... 11 more
09-06 03:26:14.952: E/AndroidRuntime(2212): Caused by: java.lang.reflect.InvocationTargetException
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.widget.ScrollView.<init>(ScrollView.java:133)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at java.lang.reflect.Constructor.constructNative(Native Method)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
09-06 03:26:14.952: E/AndroidRuntime(2212):     ... 22 more
09-06 03:26:14.952: E/AndroidRuntime(2212): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.graphics.Bitmap.nativeCreate(Native Method)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.view.View.<init>(View.java:1885)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.view.ViewGroup.<init>(ViewGroup.java:291)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.widget.FrameLayout.<init>(FrameLayout.java:83)
09-06 03:26:14.952: E/AndroidRuntime(2212):     at android.widget.ScrollView.<init>(ScrollView.java:137)
09-06 03:26:14.952: E/AndroidRuntime(2212):     ... 26 more

我不知道出了什么问题……感谢您的帮助……

4

1 回答 1

1

映像的文件大小(在这种情况下 > 200 KB)与加载它所需的 VM 数量几乎没有关系。先验确定所需内存量的最佳方法是使用图像的高度和宽度,以及它是否具有 Alpha 通道。

如果您有一个尺寸为 400 x 800、alpha 通道和每通道 8 位 (ARGB_8888) 的图像,则在 VM 中将需要 400 x 800 x 4 字节来加载图像。

例如,您可以以编程方式使用 BitmapFactory.decodexxxx(xxxx, options),其中 options 设置为仅解码,然后读取图像的实际宽度和高度。如果图像太大,您可以使用相同的 BitmapFactory 方法,并设置选项来缩放图像。

这是一个使用 InputStream 加载图像的示例

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream inputStream = context.getContentResolver()
                .openInputStream(imageUri);
        BitmapFactory.decodeStream(inputStream, null, options);
        inputStream.close();
        int imageWidth = options.outWidth;
        int imageHeight = options.outHeight;

然后根据图像的大小,您可以使用相同的选项在加载时对其进行缩放:

options.inJustDecodeBounds = false;
        options.inSampleSize = sampleSize;
        inputStream = context.getContentResolver()
                .openInputStream(imageUri);
        bitmap = BitmapFactory.decodeStream(inputStream, null, options);
        inputStream.close();
于 2012-09-05T22:40:53.513 回答