0

そのため、同じレイアウトのボタンの下に ListView を配置しようとしています。基本的に、ListView オプションを選択すると、選択したオプションごとにボタンの反応が異なります。問題は、強制的に閉じられていることであり、ListView の設定に問題があるようです。

    <?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"
    android:background="@drawable/background"
    >
<Button
    android:background="@drawable/button"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp" 
    android:layout_gravity="center"
    android:id="@+id/button"/>

    <ListView
    android:listSelector="#990000"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:id="@+id/listview"
    />
</LinearLayout>

コード:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    lv = (ListView) findViewById(R.id.listview);

    ArrayList<String> list = new ArrayList<String>();
    soundsList.add("one");
soundsList.add("two");
soundsList.add("three");



    ArrayAdapter<String> arrayAdapter =      
             new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list);
             lv.setAdapter(arrayAdapter);

Logcat の読み取り値は次のとおりです。

08-23 10:21:23.735: ERROR/AndroidRuntime(588): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

私はArrayAdapterでそれが言うことを正確に試しました...(android.R.id.list。ListActivityを拡張するなど、他にも多くのことを試しましたが、何を試しても同様のLogcat応答が得られるようです.

私が達成しようとしていることをやってのけるより良い方法はありますか? ListView の上にあるボタンをすべて同じ main.xml に配置したい。

前もって感謝します。

編集:

私もこのコードを試しました。現在はコメントアウトされていますが、元々やってみたものでした。

setListAdapter(new ArrayAdapter<String>(this, android.R.id.list,LIST));

    ListView listView = getListView();
    listView.setTextFilterEnabled(true);

LIST は文字列配列です。上記のコードからの Logcat の出力は、前と同じでした。

4

1 に答える 1

1

ListActivity または ListFragment を実装しようとしているように聞こえますが、これは特定の名前のリストビュー インスタンスを見つけることを期待しているため、エラーが発生します。リストビューのIDを次から変更してみてください:

android:id="@+id/listview"

に:

android:id="@android:id/list"

すでにこれを試したとあなたが言ったことは知っていますが、コードを考えると、まだ間違っているようです。変更したら、新しいエラーメッセージを投稿できますか? あなたの説明によれば、それは同様のメッセージになると思いますが、違いがわずかであっても、おそらくまだ重要です.

編集:

もう 1 つ気付きました。実際に ListActivity または FragmentActivity を使用している場合は、getListView() instead offindViewById()` を呼び出して ListView への参照を取得します。

于 2012-08-23T17:48:43.147 に答える