1

The code is like the following in onResume of an activity:

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

The component of Layout image_view related to the crash is the following:

<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" />

The resolution of the image file ic_launcher_background.png is 399x399.

The occasional crash report is the following:

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)

I want to emphasize that this happens rarely, but it cannot be caught by the catch block of the code, so it crashes the app.

Could anyone shed some light on this?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Hong
  • 17,643
  • 21
  • 81
  • 142

1 Answers1

-2
<?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>

Cause by "java.lang.OutOfMemoryError" I never face this problem before, just hope can help you...

  1. Setting "android:largeHeap=true" in your manifests file. ( the easiest way to solve OOM error)
  2. Try to use smaller image if it is a memory-related issue.