Android inflate会导致java.lang.OutOfMemoryError偶尔出现

时间:2016-03-31 20:29:33

标签: android android-layout out-of-memory android-imageview android-inflate

代码与onResume中的以下内容类似:

for (int i = 0; i < 4; i++) {
    LayoutInflater.from(this).inflate(R.layout.image_view, gridLayout);
    ...
}

与崩溃相关的Layout image_view组件如下:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="0dip"
    android:padding="0dip"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_launcher_background" />

图像文件ic_launcher_background.png的分辨率为399x399。

偶尔的崩溃报告如下:

java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
    at android.content.res.Resources.loadDrawable(Resources.java:3022)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
    at android.widget.ImageView.<init>(ImageView.java:133)
    at android.support.v7.widget.ar.<init>(SourceFile:58)
    at android.support.v7.widget.ar.<init>(SourceFile:54)
    at android.support.v7.a.au.a(SourceFile:95)
    at android.support.v7.a.af.c(SourceFile:938)
    at android.support.v7.a.af.a(SourceFile:992)
    at android.support.v4.g.ad.onCreateView(SourceFile:44)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at net.mydomain.app.fooActivity.onResume(SourceFile:609)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1210)
    at android.app.Activity.performResume(Activity.java:5468)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2873)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2912)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2345)
    at android.app.ActivityThread.access$800(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5293)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)

我想强调这种情况很少发生,但它不能被代码的catch块捕获,因此它会使应用程序崩溃。

有人可以对此有所了解吗?

1 个答案:

答案 0 :(得分:-1)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chingjing.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true" //Add this
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

原因是“java.lang.OutOfMemoryError” 我以前从未遇到过这个问题,希望可以帮助你...

  1. 在清单文件中设置"android:largeHeap=true"。 (解决OOM错误的最简单方法)
  2. 如果是与内存相关的问题,请尝试使用较小的图片。