Out of memory on a 3686416-byte allocation, android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>

StackOverflow https://stackoverflow.com/questions/17653892

Question

I am getting multiple errors after adding a background image only to the parent RelativeLayout after calling inside an Asynctask (This error goes out using emulator only but when used with actual device, the stack trace is not displaying anything or is fine):

public class UserAuthTask extends AsyncTask<String, Void, String> {
    private boolean success = false;

    @Override
    protected String doInBackground(String... path) {
        // TODO: attempt authentication against a network service.
        try {
            // Simulate network access.
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return null;
        }

        Log.d(Constant.TAG_AUTH, path[0]);
        String apiRequestReturn = WebServiceUtil.getRequest(path[0]);
        if (apiRequestReturn.equals("")) {
            Log.d(Constant.TAG_AUTH, "WebService request is null");
            return null;
        } else {
            Log.d(Constant.TAG_AUTH, "WebService request has data");
            return apiRequestReturn;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        userAuthTask = null;
        showProgress(false);

        super.onPostExecute(null);

        if (Boolean.parseBoolean(result) == true) {
            // Account exists, return true if the password matches.
            success  = true;
        }

        if (success) {
            // Start main activity if auth is successful
            Intent i = new Intent(AuthActivity.this, NextActivity.class);
            startActivity(i);

            application.shortToast(getApplicationContext().getResources().getString(R.string.auth_success));
            Log.d(Constant.TAG_AUTH, "Authentication successful");
        } else {
            application.shortToast(getApplicationContext().getResources().getString(R.string.invalid_account));
            Log.d(Constant.TAG_AUTH, getString(R.string.invalid_account));
            Log.d(Constant.TAG_AUTH, "Authentication failed");

            finish();
        }
    }

    @Override
    protected void onCancelled() {
        userAuthTask = null;
        showProgress(false);
    }
}

This is the NextActivity:

public class NextActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.next, menu);
    return true;
}}

AND the CAUSE of the errors is the line android:background="@drawable/master_background" at activity_next.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/master_background" >

This is only an image with color brown, I just have to use this image to use the resources(images) given by my designer. I just don't understand when I removed the line android:background="@drawable/master_background" my code goes fine. By the way here's the stack trace:

    07-15 11:51:20.616: E/AndroidRuntime(2392): FATAL EXCEPTION: main
    07-15 11:51:20.616: E/AndroidRuntime(2392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample/com.sample.NextActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.os.Looper.loop(Looper.java:137)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at dalvik.system.NativeStart.main(Native Method)
    07-15 11:51:20.616: E/AndroidRuntime(2392): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.Activity.setContentView(Activity.java:1881)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at com.sample.NextActivity.onCreate(NextActivity.java:12)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.Activity.performCreate(Activity.java:5104)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.Instrumentation.callActivityOnCreate(Insttrumentaion.java:1080)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     ... 11 more
    07-15 11:51:20.616: E/AndroidRuntime(2392): Caused by: java.lang.reflect.InvocationTargetException
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at java.lang.reflect.Constructor.constructNative(Native Method)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     ... 23 more
    07-15 11:51:20.616: E/AndroidRuntime(2392): Caused by: java.lang.OutOfMemoryError
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.content.res.Resources.loadDrawable(Resources.java:1965)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.View.<init>(View.java:3330)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.View.<init>(View.java:3259)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.view.ViewGroup.<init>(ViewGroup.java:425)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:210)
    07-15 11:51:20.616: E/AndroidRuntime(2392):     ... 26 more

EDIT: The image size of master_background is 1080*1920 with size of 1.01 MB, does this affect the "out of memory" error? Since this only comes out in emulator but not in the actual device. I just want an explanation to this problem. Thank you very much.

Était-ce utile?

La solution

Check this Android developer link. And have the images in appropriate folder with appropriate sizes. When you don't have images in appropriate size, the os will try to adjust the image size itself and that causes out of memory exception.

https://developer.android.com/guide/practices/screens_support.html

Autres conseils

Just because the image file size is 1 MB on disk, it doesn't mean that it will occupy the same amount of memory in memory.

When that image file is converted into a Drawable for adding as the background, it'll occupy a much larger amount of memory. What you need to do is -

Make resized versions(320*480, 480*800, 720*1280) sized images and put them in drawable-mdpi, -hdpi and -xhdpi respectively. You shouldn't be needing higher resolution images for backgrounds.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top