Binary XML file line #19: Attempt to invoke virtual method 'boolean java.lang.String.equals(java....

APP中需要实现LayoutInflater布局加载器动态加载布局,然而开启程序一运行就闪退。。。

9125154-322f425a339ea3d0.png

9125154-c2361d2edde902f9.png
FATAL EXCEPTION: main
                                                 Process: com.lwp.justtest, PID: 6054
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lwp.justtest/com.lwp.justtest.MainActivity}: android.view.InflateException: Binary XML file line #19: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                  Caused by: android.view.InflateException: Binary XML file line #19: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:761)
                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                     at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                     at com.lwp.justtest.MainActivity.onCreate(MainActivity.java:21)
                                                     at android.app.Activity.performCreate(Activity.java:6662)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

于是上优快云扒博文(参考:https://blog.youkuaiyun.com/qq_36408196/article/details/79968169),结果发现,这个坑相当有毒。。。

9125154-6b9c91e624659ada.png
错误示范。。。

问题就在控件View的书写,View的首字母要大写,要大写,大写,写。。

9125154-fd8866d725d27ee5.png

呐,改成上图这样即可了。

在 Android 应用开发中,如果在启动某个 Activity 时出现 `java.lang.RuntimeException: Unable to start activity ComponentInfo`,并伴随 `android.view.InflateException` 和提示“layout_width attribute missing”,这通常意味着某个视图组件在 XML 布局文件中缺少 `layout_width` 属性定义,从而导致视图无法正确初始化并引发崩溃。 此类问题通常出现在自定义 View 或第三方库的组件中,尤其是在使用了 `android.support.v7.widget.RecyclerView` 或 `androidx.recyclerview.widget.RecyclerView` 等组件时未正确设置布局参数。如果某个组件的 XML 定义中缺少 `android:layout_width` 和 `android:layout_height` 属性,系统在解析布局时将抛出 `InflateException` 异常[^2]。 ### 解决方法 1. **检查 XML 布局文件** 确保所有视图组件(包括 `View`、`ViewGroup`、自定义组件)都明确设置了 `android:layout_width` 和 `android:layout_height` 属性。例如: ```xml <View android:id="@+id/divider" android:background="#CCCCCC" android:layout_width="match_parent" android:layout_height="1dp" /> ``` 2. **检查自定义组件** 如果使用了自定义 View,在 XML 中使用时也必须指定 `layout_width` 和 `layout_height`,否则也会导致异常。例如: ```xml <com.example.myapp.CustomView android:layout_width="wrap_content" android:layout_height="wrap_content" /> ``` 3. **使用正确的命名空间** 如果是自定义属性或使用了支持库组件(如 `android.support.v7.widget.RecyclerView`),请确保在根布局中声明了正确的命名空间: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> ``` 4. **更新依赖库版本** 如果问题出现在第三方库中(如引用中提到的 `PicturePreviewActivity`),请确认使用的库版本是否为最新稳定版,或查看其 GitHub Issues 页面是否已有类似问题报告。 5. **日志分析与调试** 查看 Logcat 输出,定位具体的 XML 文件和行号信息。异常信息中通常会指出具体的文件路径和错误位置,例如: ``` Binary XML file line #8: Error inflating class ``` 上述信息表明问题出现在 XML 文件第 8 行定义的视图组件。 ### 示例代码:正确设置 layout_width 和 layout_height ```xml <androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌川江雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值