2017-10-11 7 views
0

私はにカスタムviewを実装しています。私はいつもそれを次のように作成していました:Androidカスタムビュー:XML対Progrmatically

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.view1); 
CustomView customView = new CustomView(relativeLayout.getContext()); 
relativeLayout.addView(customView); 

これはうまくいきます。 XMLで

CustomView customView = (CustomView) findViewById(R.id.customView);

それはこのようなものだ:私は別の何かしようとしたときしかし android.view.InflateException:バイナリXMLファイルのライン#14

<com.my.package.CustomView 
     android:id="@+id/customView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

を私はエラーを持っています:クラスを膨張させるエラー

私のカスタムビューはRelativeLayoutを延長し、これは唯一のコンストラクタです:

public CustomView(Context c){ 
    super(c); 
    //adding some html to the webview here 
} 

私に何かが不足していますか?

+0

はhttp://www.vogella.com/tutorials/AndroidCustomViews/article.htmlを参照してください。 –

答えて

2

XMLからカスタムビューを使用すると、単一の引数コンストラクタは使用されません。むしろ、2つまたは3つの引数コンストラクタが使用されます。

あなたは同様に他のコンストラクタを追加する必要があります

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

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

public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 
関連する問題