MrPencil
MrPencil

Reputation: 964

android.view.InflateException: Binary XML file line #3: Error inflating class <unknown>

I am trying to put a splash image 4.41 KB that I made with my Samsung S4 device to my app but I am getting the error below when I store it in this directory drawable-hdpi. I have even tried to store it in the ldpi, mdpi, xhdpi and xxhdpi in this case I am getting a white splash background without image.

I downloaded an image from the internet that has 800x600 pixel size and 494 KB. I put it just in the drawable-hdpi directory and it works (The image is being dispalyed as splash) but I want to put my own S4 device image as background. How can I fix it?

XML file:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
     android:background= "@drawable/splash_background"
    >

</LinearLayout>

Error:

08-02 12:24:01.781: E/AndroidRuntime(15857): FATAL EXCEPTION: main
08-02 12:24:01.781: E/AndroidRuntime(15857): Process: com.bustracker, PID: 15857
08-02 12:24:01.781: E/AndroidRuntime(15857): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bustracker/com.bustracker.Splash}: android.view.InflateException: Binary XML file line #3: Error inflating class <unknown>
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.app.ActivityThread.access$900(ActivityThread.java:177)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.os.Looper.loop(Looper.java:145)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.app.ActivityThread.main(ActivityThread.java:5944)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at java.lang.reflect.Method.invoke(Native Method)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at java.lang.reflect.Method.invoke(Method.java:372)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
08-02 12:24:01.781: E/AndroidRuntime(15857): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class <unknown>
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.view.LayoutInflater.createView(LayoutInflater.java:640)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at com.bustracker.Splash.onCreate(Splash.java:13)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.app.Activity.performCreate(Activity.java:6289)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
08-02 12:24:01.781: E/AndroidRuntime(15857):    ... 10 more
08-02 12:24:01.781: E/AndroidRuntime(15857): Caused by: java.lang.reflect.InvocationTargetException
08-02 12:24:01.781: E/AndroidRuntime(15857):    at java.lang.reflect.Constructor.newInstance(Native Method)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.view.LayoutInflater.createView(LayoutInflater.java:614)
08-02 12:24:01.781: E/AndroidRuntime(15857):    ... 22 more
08-02 12:24:01.781: E/AndroidRuntime(15857): Caused by: java.lang.OutOfMemoryError: Failed to allocate a 153363468 byte allocation with 8388608 free bytes and 112MB until OOM
08-02 12:24:01.781: E/AndroidRuntime(15857):    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:726)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:547)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1014)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.content.res.Resources.loadDrawableForCookie(Resources.java:3730)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.content.res.Resources.loadDrawable(Resources.java:3603)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.content.res.TypedArray.getDrawable(TypedArray.java:762)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.view.View.<init>(View.java:3970)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.view.ViewGroup.<init>(ViewGroup.java:507)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.widget.LinearLayout.<init>(LinearLayout.java:199)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.widget.LinearLayout.<init>(LinearLayout.java:195)
08-02 12:24:01.781: E/AndroidRuntime(15857):    at android.widget.LinearLayout.<init>(LinearLayout.java:191)
08-02 12:24:01.781: E/AndroidRuntime(15857):    ... 25 more

Upvotes: 3

Views: 2530

Answers (3)

Saddan
Saddan

Reputation: 1655

I assume may be android:background= "@drawable/splash_background" this line causing you this exception because you put your splash_background in drawable-v24.Move it to drawable.And the reason you're getting this exception because this folder items only place for API level 24 or higher but your phone or emulator is may be lower than this.So it's a good practice to put your drawable resource file only in drawable folder.

enter image description here

Upvotes: 1

Amey Shirke
Amey Shirke

Reputation: 734

The image you are trying to put might have a high resolution. This seems to be the main cause of OutOfMemoryError.

You can use Android Asset Studio to create images for various screen densities. https://stackoverflow.com/questions/10754523/is-there-any-tool-which-can-a-resize-png-image-into-a-image-for-ldpi-mdpi-hdp

Upvotes: 1

Hussein El Feky
Hussein El Feky

Reputation: 6707

You can do a tricky thing if you want to avoid that OutOfMemoryError.

Go to your AppManifest.xml and in your application tag, add this:

android:largeHeap="true"

This piece of code will try to avoid you from the OutOfMemoryError. It increases your application priority. When any user has many apps opened, this will try to clear some memory from other apps to free some more space for your app. But still try to shrink your image's file size as that will be definitely better. Check this link. I always use that tool: http://www.compresspng.com

Also make sure that you have registered your new activity in your AppManifest.xml Even the best developers sometimes forget to do this.

If you still face OutOfMemoryError then try to do like what Amey Shirke has said.

Upvotes: 1

Related Questions