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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

</LinearLayout> 

上記は私のレイアウトです " messages.xml"

public class TabClass extends ListFragment {    

    @Override
      public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] values = new String[] { "label1", "label2", "label3"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            R.layout.messages, values);
        setListAdapter(adapter);
      }

      @Override
      public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something with the data

      }

}

アプリケーションをコンパイルしようとすると、このエラーが発生します"ArrayAdapter requires the resource ID to be a TextView"

4

1 に答える 1

2

messages.xml には、ImageView と TextView を含む LinearLayout が含まれています。代わりに、TextView だけを含める必要があります。こちらのドキュメントを参照してください。ArrayAdapter コンストラクターの 2 番目のパラメーターは次のようになります。

ビューをインスタンス化するときに使用する TextView を含むレイアウト ファイルのリソース ID。

ヒント: ArrayAdapter クラスの代わりに、(BaseAdapter を拡張する) カスタム アダプター クラスを作成することをお勧めします。

于 2013-03-20T14:27:35.560 に答える