Android inflate()引发NullPointerException

使用LayoutInflaterinflate()方法渲染View

1
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog, null);

报了一个看似毫无关联的空指针异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:719)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at com.android.internal.app.AlertController.setupView(AlertController.java:482)
at com.android.internal.app.AlertController.installContent(AlertController.java:239)
at android.app.AlertDialog.onCreate(AlertDialog.java:356)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at ch.appblog.test.ui.activity.MainActivity$3.onCreate(MainActivity.java:86)
at android.view.View.performClick(View.java:4811)
at android.view.View$PerformClick.run(View.java:20136)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

通过排查布局文件,

1
2
3
4
<view
style="@style/Line_View"
android:layout_height="1dp"
/>

原因:<view>标签的v是小写的,应该是<View>,资源文件编译未通过,不过viewView真的比较容易搞错,而且Android Studio也不会提示错误

所以编译进程卡在这里,布局资源控件未识别,inflate()引用了一个空的布局文件id

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :