2014-10-16 109 views
0

我想添加一個ListView和自定義視圖在我的佈局,但我總是得到這個錯誤:安卓:自定義視圖在XML的佈局

」 ...錯誤充氣類com。示例.. 。「

沒有自定義視圖它的作品,只顯示ListView。


活動:

package com.example.training; 

public class PracticeActivity extends ActionBarActivity{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.practice_layout); 
     initListView(); //private function 
    } 
    //Other stuff 
} 

自定義視圖:

package com.example.training; 

public class LinearChart extends View{ 

    LinearChart(Context context, AttributeSet attrs){ 
     super(context,attrs); 
    } 

    LinearChart(Context context){ 
     super(context); 
    } 

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

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.drawColor(Color.WHITE); 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLUE); 
     paint.setStrokeWidth(10); 
     canvas.drawLine(10, 10, 30, 30, paint); 
    } 
} 

XML的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:id="@+id/practice_listview" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:layout_centerHorizontal="true" > 
    </ListView> 

    <view 
     android:id="@+id/view1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/practice_listview" 
     class="com.example.training.LinearChart" /> 

</RelativeLayout> 
+0

確保清理項目以刷新生成的文件。 – 2014-10-16 10:15:56

回答

1

你班上沒有公共構造函數。確保你重寫構造函數。它應該是這樣的,而不是:

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

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

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

通知public改性劑的成癮性。

+0

不能相信這是解決方案:D非常感謝 – S1J0 2014-10-16 10:36:13

+0

您可以檢查您的logcat以獲取更多信息。通常這會拋出'引發:java.lang.NoSuchMethodException: [類的android.content.Context,接口android.util.AttributeSet]'這告訴你,沒有找到構造函數。默認訪問修飾符是私人的,所以'LayoutInflater'找不到它。祝你有美好的一天。 – 2014-10-16 10:40:17

0
<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ListView 
    android:id="@+id/practice_listview" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:layout_centerHorizontal="true" > 
</ListView> 

<com.example.training.LinearChart 
    android:id="@+id/view1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/practice_listview" 
    /> 

    </RelativeLayout> 
+1

與OP的代碼完全相同。 – 2014-10-16 10:14:50

+0

給我看例外。但首先清理該項目,並通過將我的代碼放入xml來重建它。 – 2014-10-16 10:17:51

0

而不是<view在您的xml文件中,您需要該類的完全限定名稱(或使用新的xml名稱空間)。所以,在這個例子中,你的XML可能看起來像:

<com.example.training.LinearChart 
    android:id="@+id/view1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

(因此刪除類:屬性,在當前的XML)。然後這應該膨脹正確。

+1

這是一回事。你不需要這樣做。 – 2014-10-16 10:24:31