helloWorld XML レイアウトでカスタム ビューを参照しようとしていますが、次の例外が発生します: クラス acme.my.MyTextView の拡張中にエラーが発生しました。
ただし、ビューをインスタンス化してメイン コンテンツ ビューに手動で追加することはできます。カスタム ビューは、独自の XML レイアウトから構築されます。これを機能させるにはどうすればよいですか?
public class MyTextView extends LinearLayout {
MyTextView(Context context){
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_text_view,this);
}
MyTextView(Context context, AttributeSet attrs){
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_text_view,this);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<view class = "acme.my.MyTextView"
android:id="@+id/myView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>