android.view.inflateexception binary xml file line 异常的解决方法

来源:互联网 发布:匿名朋友圈源码 编辑:程序博客网 时间:2024/05/21 09:14

有时候一个很简单的xml布局文件,运行却抛出以下异常:

07-25 10:40:50.966: D/AndroidRuntime(31570): Shutting down VM
07-25 10:40:50.966: W/dalvikvm(31570): threadid=1: thread exiting with uncaught exception (group=0x42441700)
07-25 10:40:50.976: E/AndroidRuntime(31570): FATAL EXCEPTION: main
07-25 10:40:50.976: E/AndroidRuntime(31570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.moveball/com.example.moveball.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class com.example.moveball.DrawView
07-25 10:40:50.976: E/AndroidRuntime(31570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
07-25 10:40:50.976: E/AndroidRuntime(31570): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
07-25 10:40:50.976: E/AndroidRuntime(31570): at android.app.ActivityThread.access$700(ActivityThread.java:168)
07-25 10:40:50.976: E/AndroidRuntime(31570): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)

出现这种异常,一般是与相应的xml的标签和属性的命名、定义有关。

有时候很小的错误就会产生这种问题,这种小错误一般很难检查,所以在写代码的时候就要有所注意,免得之后检查起来麻烦。

比如:控件EditText写成了TextView等小问题

又如:没有找到资源文件。

提示:可以参考我的另外一篇文章《Android资源文件夹及资源文件的详细介绍》

一般看看res/下面的资源文件,比如全放在drawable-mdpi/目录下,,将drawable-mdpi/下的资源文件拷贝一份到drawable-ldpi/目录下,还是报错误,再拷贝一份到drawable-hdpi/目录下,问题解决。

要经常怀疑寻找的位置资源文件不存在。

一般在res/下建一目录drawable/,将drawable-mdpi/下所有的资源文件都拷贝到drawable/下即可。

这些类似的等等资源文件的错误需要注意。

总结一下xml文件经常容易犯的低级错误:

1. 控件名称不能写错

2.名称的大小写要区分,如EditText与editText是完全不一样的

3.标签一定是成对出现的,尤其是嵌套布局

4.属性前面一般要加android:

5.id比较特殊,应该是@+id ,其它的直接加@即可,如@string

6.drawable中引用的图片资源不存在或名称大小写有误

 

此外,出现这种异常还可能与自定义的View类有关,需要增加一个带属性的构造函数

抛出异常时的main.xml与自定义View类相关代码如下:

activity_main.xml:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <com.example.moveball.DrawView        android:id="@+id/drawView"        android:layout_width="match_parent"        android:layout_height="match_parent" >    </com.example.moveball.DrawView></RelativeLayout></span>


继承View的类的自定义View:

<span style="font-size:14px;">public class DrawView extends View{private float circleX = 40;private float circleY = 50;private float circleR = 15;//构造方法public DrawView(Context context){super(context);}//重写ondraw方法   下面代码省略了...</span>



对于此异常,如下进行修改:添加View类的另一个构造方法即可解决问题!

<span style="font-size:14px;">public class DrawView extends View{private float circleX = 40;private float circleY = 50;private float circleR = 15;//构造方法public DrawView(Context context,AttributeSet attrs){super(context,attrs);}</span>


总之抛出这种异常的原因有可能是必须实现的构造器没有实现:

须实现三个构造函数
    public GalleryFlow(Context context) {
            super(context);
    }

    public GalleryFlow(Context context, AttributeSet attrs) {
            super(context, attrs);
    }

    public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    }



3 0
原创粉丝点击