-2

ListActivity を起動するボタンがありますが、それをクリックするたびに、次の LogCat エラーでアプリがクラッシュします。

01-10 13:12:51.327: E/AndroidRuntime(2970): FATAL EXCEPTION: main
01-10 13:12:51.327: E/AndroidRuntime(2970): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.app/com.app.Settings}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

これはxmlコードです:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background"
android:orientation="vertical"
android:padding="15dip" >

<ListView
    android:id="@+id/listViewSettings"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

そして、これはJavaコードです:

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class Settings extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        ArrayList<String> listItems=new ArrayList<String>();
        listItems.add("foo");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                listItems);
            setListAdapter(adapter);
    }

}

リストIDをに変更しようとしましたlistが、これを行うと次のエラーが発生します。

If I do this I get the following error: `Error: No resource found that matches the given name (at 'id' with value '@id/list').`
4

3 に答える 3

2

XML のこの行:

android:id="@+id/listViewSettings"

...次のようにする必要があります。

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

なんで?では、を維持したい場合ListActivityは、ID " " を使用する必要があります( を使用する場合も同じことが当てはまります)。android.R.id.listListViewListFragment

于 2013-01-10T12:20:49.803 に答える
2

ListActivityListViewにデフォルトでsetContentView()ListView が含まれていることを表示するだけの場合は、メソッドを使用してレイアウトを割り当てる必要はありません。ListActivity

ListView よりも多くのビューを含める必要がある場合ListActivityでも、アクティビティにレイアウトを割り当てることができます。この場合、レイアウトには、android:id属性が に設定された ListView が含まれている必要があります@android:id/list

リストIDを次のように変更します。

android:id="@android:id/list"
于 2013-01-10T12:22:18.157 に答える
1

xmlを変更するだけですListView-id

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

この場合、レイアウトListViewにはandroid:id属性が設定された が含まれている必要があります@android:id/list

于 2013-01-10T12:24:22.903 に答える