android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
Asked Answered
S

4

25

I am developing a simple android apllication and just completed the home screen. Now if the orientation of the screen changes more than two times, it is throwing the error and application is getting crashed abrubptly.

Here is my Java Class:

public class PasswordActivity extends Activity implements OnClickListener {
  Button login;  
  Button forgot;
  Button register;  

  private static final String PREFERENCES = "prefs";
  private static final String PREFERENCES_NAME = "pref_name"; 
  SharedPreferences settings;
  private Cursor c;

  @Override 
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main); 
      login=(Button)findViewById(R.id.login_login);
      login.setOnClickListener(this);
      register=(Button)findViewById(R.id.login_register);
      register.setOnClickListener(this);
     
  }
  public void onClick(View v) {
  } 
}

And this is my Xml Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"    
  android:background="@drawable/listpic">

  <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="5dp"
    android:paddingLeft="3dp"
    android:paddingRight="3dp">  

    <TextView 
     android:layout_width="fill_parent"
     android:layout_weight="0.75"
     android:layout_height="wrap_content"
     android:text="@string/login_user_name"
     android:textStyle="bold"/>

    <EditText 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_weight="0.25"
     android:id="@+id/login_user_name"
     android:inputType="text"/>
  </LinearLayout>

  <LinearLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:paddingLeft="3dp"
     android:paddingRight="3dp">  

     <TextView 
      android:layout_width="fill_parent"
      android:layout_weight="0.75"
      android:layout_height="wrap_content"
      android:text="@string/login_password"
      android:textStyle="bold"/>

     <EditText 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="0.25"
      android:inputType="textPassword"
      android:id="@+id/login_password"/>
  </LinearLayout>

  <LinearLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:paddingLeft="3dp"
     android:paddingRight="3dp">  

     <Button 
      style="@style/left_button"
      android:text="@string/login_submit"
      android:id="@+id/login_login"/>

     <Button 
       style="@style/right_button"
       android:id="@+id/login_register"
       android:text="@string/register"/>

  </LinearLayout>
</LinearLayout>

Log Cat Details:

10-21 12:05:59.982: D/dalvikvm(622): GC_EXTERNAL_ALLOC freed 774 objects / 56240 bytes in 61ms
10-21 12:06:15.031: D/dalvikvm(622): GC_EXTERNAL_ALLOC freed 737 objects / 30992 bytes in 59ms
10-21 12:06:18.022: E/dalvikvm-heap(622): 7596000-byte external allocation too large for this process.
10-21 12:06:18.022: E/GraphicsJNI(622): VM won't let us allocate 7596000 bytes
10-21 12:06:18.043: D/AndroidRuntime(622): Shutting down VM
10-21 12:06:18.043: W/dalvikvm(622): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-21 12:06:18.092: E/AndroidRuntime(622): FATAL EXCEPTION: main
10-21 12:06:18.092: E/AndroidRuntime(622): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ravi.password/com.ravi.password.PasswordActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-21 12:06:18.092: E/AndroidRuntime(622):  at java.lang.reflect.Method.invokeNative(Native Method)
10-21 12:06:18.092: E/AndroidRuntime(622):  at java.lang.reflect.Method.invoke(Method.java:521)
10-21 12:06:18.092: E/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-21 12:06:18.092: E/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-21 12:06:18.092: E/AndroidRuntime(622):  at dalvik.system.NativeStart.main(Native Method)
10-21 12:06:18.092: E/AndroidRuntime(622): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.view.LayoutInflater.createView(LayoutInflater.java:513)
10-21 12:06:18.092: E/AndroidRuntime(622):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-21 12:06:18.092: E/AndroidRuntime(622):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.app.Activity.setContentView(Activity.java:1647)
10-21 12:06:18.092: E/AndroidRuntime(622):  at com.ravi.password.PasswordActivity.onCreate(PasswordActivity.java:34)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-21 12:06:18.092: E/AndroidRuntime(622):  ... 12 more
10-21 12:06:18.092: E/AndroidRuntime(622): Caused by: java.lang.reflect.InvocationTargetException
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.widget.LinearLayout.<init>(LinearLayout.java:115)
10-21 12:06:18.092: E/AndroidRuntime(622):  at java.lang.reflect.Constructor.constructNative(Native Method)
10-21 12:06:18.092: E/AndroidRuntime(622):  at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.view.LayoutInflater.createView(LayoutInflater.java:500)
10-21 12:06:18.092: E/AndroidRuntime(622):  ... 22 more
10-21 12:06:18.092: E/AndroidRuntime(622): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.graphics.Bitmap.nativeCreate(Native Method)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
10-21 12:06:18.092: E/AndroidRuntime(622):  at android.view.ViewGroup.<init>(ViewGroup.java:285)
10-21 12:06:18.092: E/AndroidRuntime(622):  ... 26 more 

Images used are of very small size around 5kb.

Stank answered 21/10, 2012 at 8:32 Comment(0)
L
73

At runtime, Android resize's images based on need (based on the screen size and resolution). It uses Bitmap's for doing the resizing internally. Which, obviously, is very memory intensive (kinda allocates memory like crazy). One quick way to fix such issues can be, that you copy all drawable files to drawable-ldpi, mdpi, hdpi folders. This way android will just pick up the files from these folders and not resize them.

Its a random kind exception, a real pain to fix.

Lorollas answered 21/10, 2012 at 8:36 Comment(12)
Thank you.. As you said i have copied all files to drawable-hdpi folder. It's working fine now..Stank
I have the same problem, and cannot see how this is in any way connected to images or resizing.Undistinguished
Unable to start activity is connected to the setContentLayout call. And when the activity starts it loads the layout since setContentLayout says so. And as it loads, it looks for images and the rules the layout parameters define it to be. As the Android system decides what it needs to do with images, it starts to resize some of them to fit the rules you have specified in the layout parameters. Need I say more ?Lorollas
@Ravindra, any reason why you choose not to mark this answer as accepted, even after it has solved your issue ?Lorollas
but after adding all the drwables in ldpi,mdpi,hdpi and xdpi still its showing the same android inflate exception any idea.Awhile
I had same problem with images, copying images into ldpi,mdpi,hdpi and xdpi solved my problem. Thank you. This answer should be accepted.Cowled
@Siddharth, hi, i have same problem - can i just copy all ORIGINAL images into xdpi,mdpi,ldpi folders without CHANGING their sizes/pixels? does putting them into ldpi folder make them automatically fit pixel size?Pectase
@gurcan15, if you just put same images in different folders - does it help? or you have to resize them to be really ldpi images?Pectase
Just copy them to solve the problem listed in this Q. But resizing them will help with reducing the size of your apk. This problem is solved by just copying them.Lorollas
Note: You have to manually create the folders for each size, and then copy paste the images in them. It is useful to first just reduce the size of the images and copy paste.Judaize
You should use android ui tools for this. It resizes the icons and copies it into the relevant folders too. Damn neat tools.Lorollas
i copied all the images to directory drawable-ldpi,drawable-hdpi,drawable-mdpi,drawable-xhdpi which i created but it gave me duplicate resources error..kindly guide ..Diocesan
N
3

please follow background image size

for Supported device

LDPI:
Portrait: 200x320px
Landscape: 320x200px
MDPI:
Portrait: 320x480px
Landscape: 480x320px
HDPI:
Portrait: 480x800px
Landscape: 800x480px
XHDPI:
Portrait: 720px1280px
Landscape: 1280x720px
Noddy answered 26/12, 2014 at 8:22 Comment(0)
A
2

Sorry for my bad English

I had the same problem, if you put any "xml" files into "drawable-24" just move them to "drawable", this was the solution that i did to solve this problem

have a nice day :)

enter image description here

Antivenin answered 10/4, 2020 at 18:0 Comment(0)
C
0

As the others clarified, part of a problem was an issue related to the background image I'd set for my layout. However, getting that solved, I persistently encountered the error:

Unfortunately the application has stopped

With trial and error, I found the problem was caused by the <view> elements which after deleting them, I could eventually run the application.

Contrast answered 20/1, 2019 at 10:57 Comment(1)
your answer should be the comment as you know and mentioned it at starting of your answer As the others clarified also while answering add the necessary context , references, links to make it more readableLatricialatrina

© 2022 - 2024 — McMap. All rights reserved.