閱讀543 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android.view.inflateexception binary xml file line 異常的解決方法

有時候一個很簡單的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 ><?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:andro
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <com.example.moveball.DrawView
        android:
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.example.moveball.DrawView>


</RelativeLayout></span>


繼承View的類的自定義View:

<span >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 >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);
    }



最後更新:2017-04-03 05:39:31

  上一篇:go graph driver-device mapper-01driver初始化
  下一篇:go 牛腩新聞發布係統總結(一)——總體感受