android自定義view android.view.InflateException: Binary XML file line


Android異常匯集----1. android.view.InflateException: 

                                       Binary XML file line #6: Error inflating class com.examp..

 
 
本節正文:
    自定義view時候出現了該異常問題,在網上查了很久,然后對比代碼。
     總結網上以及自己分析問題如下,由於此異常通常出現錯誤地點不同,分析此問題步奏通常按照以下幾步即可:
 

    1). 引用類名問題

         自定義了一個View,將用於布局文件xml中(包名com.android.test,類名MyTestView),在XML作為布局元素來布局的話,

        必須使用完整路徑名:com.android.test.MyTestView,也就是包名加類名來引用。

        正確寫法:  

 

  1.  
    <com.android.test.MyTestView
  2.  
    android:id="@+id/test"
  3.  
    android:layout_width="fill_parent"
  4.  
    android:layout_height="fill_parent" />
        錯誤寫法:

 

 

  1.  
    <MyTestView
  2.  
    android:id="@+id/test"
  3.  
    android:layout_width="fill_parent"
  4.  
    android:layout_height="fill_parent" />

 

    2).構造函數問題

    自定義一個View,派生實現基類View的三個構造函數

    View(Context context)     //Simple constructor to use when creating a view from code

    View(Context context, AttributeSet attrs)     //Constructor that is called when inflating a view from XML

    View(Context context, AttributeSet attrs, int defStyle)     //Perform inflation from XML and apply a class-specific base style

    從文檔上的介紹來看,第二個和第三個構造函數對於XML這種引用方式是必須實現的,這三個構造函數應該是在不同的應用場合來實例化

    一個View對象。

 

    3).編譯的中間文件沒有清理干凈, 使用Eclipse clean 下.

 

    4).找不到資源文件:因為找不到相關的資源文件,xml 文件隱含有錯誤,確保xml 一定是對的.

 

     這上面四個分析一般會出現前兩種錯誤,一定要檢查好定義的xml 無誤,然后引用類名沒有問題。如果還是會報錯的話, 問題還是應該出現在第二步了。盡管上面四步都檢查過,回頭再仔細看。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM