文章目录

昨天遇到一个很奇葩的问题,使用了华为HMS SDK中的推送功能,然后插件布局文件中的TimePicker初始化时,会报如下错误:

1
2

android.view.InflateException: Binary XML file line #47: Error inflating class huawei.android.widget.TimePicker

很奇怪,TimePicker在布局文件中使用<TimePicker/>标签,当布局文件被inflate后,在android.widget.TimePicker前面加了一个huawei.。如果使用<android.widget.TimePicker/>标签,则不会有问题;而其他有地方会出现这个新的错误:

1
2

android.view.InflateException: Binary XML file line #20: Error inflating class <unknown> at android.view.LayoutInflater.createView(LayoutInflater.java:620) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) at android.view.LayoutInflater.inflate(LayoutInflater.java:469) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.app.TimePickerDialog.<init>(TimePickerDialog.java:103) at android.app.TimePickerDialog.<init>(TimePickerDialog.java:74) at android.view.View.performClick(View.java:4447) at android.view.View$PerformClick.run(View.java:18457) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5119) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at android.view.LayoutInflater.createView(LayoutInflater.java:594) ... 20 more Caused by: java.lang.NullPointerException at android.view.ViewGroup.addView(ViewGroup.java:3353) at com.huawei.android.hwcontrol.TimePickerFactory.updateAmPmStart(TimePickerFactory.java:92) at android.widget.TimePicker.<init>(TimePicker.java:265) at android.widget.TimePicker.<init>(TimePicker.java:139) ... 23 more java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at android.view.LayoutInflater.createView(LayoutInflater.java:594) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) at android.view.LayoutInflater.inflate(LayoutInflater.java:469) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.app.TimePickerDialog.<init>(TimePickerDialog.java:103) at android.app.TimePickerDialog.<init>(TimePickerDialog.java:74) at android.view.View.performClick(View.java:4447) at android.view.View$PerformClick.run(View.java:18457) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5119) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.view.ViewGroup.addView(ViewGroup.java:3353) at com.huawei.android.hwcontrol.TimePickerFactory.updateAmPmStart(TimePickerFactory.java:92) at android.widget.TimePicker.<init>(TimePicker.java:265) at android.widget.TimePicker.<init>(TimePicker.java:139) ... 23 more

但如果不使用插件,在主应用中使用TimePicker之类的组件,就一切OK。

好吧,问题很严重,已经定位到是集成了推送的功能之后出现的。但很难定位为什么出现这种问题,注册Token后,跨进程通讯问题?后台推送服务导致的?

看到这个huawei.android.widget.TimePicker在想,华为又调皮了,又改了系统的源码。因为google官方的android系统源中有这样一个逻辑,发现布局文件中的View没有包名,会自动加上android.widget.前缀。

已经在怀疑是在AndroidManifest.xml文件中配置了什么导致了问题,所以一行一行的排查。果然,在去掉下面这行后,就正常了。

1
2
3
4
5
6

<application>

<meta-data android:name="hwc-theme" android:value="androidhwext:style/Theme.Emui.NoActionBar" />

</application>

这个meta-data定义是放在applicaton标签中,所以会针对整个App。把这行干掉之后,问题解决。

这种问题也只能是在这种特定场景中被遇到,一般还真遇不到。三生有幸!

文章目录