0

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"           
 />
4

2 に答える 2

1

コンストラクターは公開されていません。:(

于 2010-07-30T11:37:06.657 に答える
0

このように呼び出す必要があります

 <com.example.MyLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

また、コンストラクターを公開します

于 2011-11-25T12:15:03.993 に答える