【问题标题】:android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
【发布时间】:2022-02-05 05:54:30
【问题描述】:

我正在开发一个简单的 android 应用程序,并且刚刚完成了主屏幕。现在如果屏幕的方向改变超过两次,它就会抛出错误并且应用程序会突然崩溃。

这是我的 Java 类:

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) {
  } 
}

这是我的 Xml 代码:

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

记录猫的详细信息:

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 

使用的图片非常小,大约 5kb。

【问题讨论】:

    标签: android android-emulator android-memory


    【解决方案1】:

    在运行时,Android 根据需要(基于屏幕大小和分辨率)调整图像大小。它使用位图在内部进行大小调整。显然,这非常占用内存(有点像疯了一样分配内存)。解决此类问题的一种快速方法是将所有可绘制文件复制到 drawable-ldpi、mdpi、hdpi 文件夹。这样 android 只会从这些文件夹中提取文件而不调整它们的大小。

    这是一个随机的异常,修复起来真的很痛苦。

    【讨论】:

    • 谢谢您.. 正如您所说,我已将所有文件复制到 drawable-hdpi 文件夹。它现在工作正常..
    • 我有同样的问题,看不出这与图像或调整大小有什么关系。
    • Unable to start activity 连接到setContentLayout 呼叫。当活动开始时,它会加载布局,因为 setContentLayout 是这样说的。当它加载时,它会查找图像和布局参数定义的规则。当 Android 系统决定它需要对图像做什么时,它会开始调整其中一些图像的大小以适应您在布局参数中指定的规则。需要我多说吗?
    • @Ravindra,您为什么选择not 将此答案标记为已接受,即使它已经解决了您的问题?
    • 但是在 ldpi、mdpi、hdpi 和 xdpi 中添加了所有 drwables 后,它仍然显示相同的 android inflate 异常任何想法。
    【解决方案2】:

    请遵循背景图片尺寸

    对于支持的设备

    LDPI:
    Portrait: 200x320px
    Landscape: 320x200px
    MDPI:
    Portrait: 320x480px
    Landscape: 480x320px
    HDPI:
    Portrait: 480x800px
    Landscape: 800x480px
    XHDPI:
    Portrait: 720px1280px
    Landscape: 1280x720px
    

    【讨论】:

      【解决方案3】:

      对不起我的英语不好

      我遇到了同样的问题,如果你把任何“xml”文件放到“drawable-24”中,只需将它们移动到“drawable” ",这是我为解决这个问题所做的解决方案

      祝你有美好的一天:)

      【讨论】:

        【解决方案4】:

        正如其他人所澄清的那样,部分问题是与我为布局设置的背景图像相关的问题。但是,解决了这个问题后,我一直遇到错误:

        不幸的是,应用程序已停止

        经过反复试验,我发现问题是由 &lt;view&gt; 元素引起的,删除它们后,我最终可以运行应用程序。

        【讨论】:

        • 您的答案应该是您所知道的评论,并在您的答案As the others clarified 开始时提到它,同时在回答时添加必要的上下文、参考、链接以使其更具可读性
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多