【安卓异常】使用自定义view控件报错Unable to start activity ComponentInfo...Didn't find class "android.view.xxView"

使用自定义控件报错信息如下:

① Unable to start activity ComponentInfo{xxxActivity}: 
android.view.InflateException: Binary XML file line #0: Binary XML file 
line #0: Error inflating class xxxView

② vjava.lang.ClassNotFoundException: Didn't find class
 "android.view.xxView" on path:....

报错原因:观察上面报错信息,可以知道在加载xml中控件时出错,xml控件对应的控件类找不到。仔细观察自定义的view控件xxView的路径不对,不在系统控件路径android.view目录下。

解决办法:在xml写自定义控件标签时写完全路径类名。如下:

<com.mapc.demo.ui.listview.MyListView
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
</com.mapc.demo.ui.listview.MyListView>

异常小贴士

由此可以知道:系统在扫描xml布局文件时,对读取到的每个标签的处理情况如下:

  1. 如果是单一类名,如ListView,系统会去系统提供的控件目录android.view下读 取并实例化该类;
  2. 如果是完全路径类名,系统则直接到指定路径下读取并实例化该类。

所以,在布局文件中使用自定义控件时一定要写完全路径类名


参考资料:

Android运行异常:Unable to start activity ComponentInfo{}: android.view.InflateException

猜你喜欢

转载自blog.csdn.net/u012995888/article/details/80500915