2013-02-11 80 views
2

我以前見過這個錯誤,但沒有一個答案似乎解決了我的問題,所以我不確定發生了什麼事情。它試圖在xml中膨脹我的類時失敗。錯誤膨脹類,自定義視圖

XML:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#EEE" 
    > 
    <android.view.SurfaceView 
     android:id="@+id/preview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     > 
    </android.view.SurfaceView> 
    <com.commonsware.android.picture.PictureDemo.DrawOnTop 
     android:id="@+id/grid" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
    </com.commonsware.android.picture.PictureDemo.DrawOnTop> 
</FrameLayout> 

類:

class DrawOnTop extends View 
    { 

     public DrawOnTop(Context context) 
     { 
      super(context); 
     } 

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

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

     } 

     @Override 
     protected void onDraw(Canvas canvas) 
     { 
      Paint paint = new Paint(); 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setColor(Color.BLACK); 
      canvas.drawText("Test Text", 10, 10, paint); 
      canvas.drawBitmap(bitmap, 0, 0, null); 
      super.onDraw(canvas); 
     } 
    } 

錯誤:

02-11 16:06:25.456: E/AndroidRuntime(11717): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.commonsware.android.picture.PictureDemo.DrawOnTop 

我所有的構造函數和沒有看到任何其他的問題,我缺少什麼?

+1

你是什麼意思「當它試圖在xml中膨脹我的類時失敗。」你可以發佈堆棧跟蹤嗎? – FoamyGuy 2013-02-11 21:20:09

+0

什麼是你的包名,它真的是包嗎com.commonsware.android.picture.PictureDemo;'? – Sam 2013-02-11 21:28:31

+0

@Sam包是com.commonsware.android.picture(它是github的一系列示例)DrawOnTop類在文件「PictureDemo」中定義我不確定是否需要在xml中但是它確實無法以任何方式工作 – jgelderloos 2013-02-11 21:33:52

回答

3

嘗試將DrawOnTop分離到同一包中的另一個文件。

+9

你能否澄清一下這是如何解決這個問題的?我收到了類似的錯誤,並且我的課程已經在同一個包中的自己的文件中。 – 2013-06-27 18:38:06

+0

@SimonSarris你解決了這個問題嗎?我遇到了同樣的問題。 – 2014-07-03 14:58:39

+0

這解決了這個問題,因爲如何選擇用於通貨膨脹的軟件包有問題(通過有問題的文件)。通過將其分離出來,您可以確保它在正確的包裝中,因爲您已經創建了它;似乎是當你導入它時,它不能正確導入包。 – JoxTraex 2014-07-04 07:05:38