class扩展了ListActivity,其id属性为'android.R.id.list'“

时间:2012-02-19 10:52:25

标签: android listview android-listview listactivity runtimeexception

我正在尝试将我的主Activity扩展为“ListActivity”(以前扩展为:Activity)以使用onListItemClick,但是logcat,发送给我“你的内容必须有一个ListView,其id属性是'android.r。 id.list'”。 (我正在使用SQLite填充ListView)。

mi xml是:

<ListView
    android:id="@+id/list" // to "@id/android:list" or other styles (but nothing works)
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/> // or closing with </ListView>

这是ListView的完整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:orientation="vertical" >
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" /></LinearLayout>

我处理ListView的主要类:

public class lay_main extends ListActivity{
public ListView list;
private DBHelper myAdap;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lay_main);

    collectXML();
    setupDataBase();
    setupAdapter();

}      
private void collectXML() 
{
    list = (ListView)findViewById(R.id.list);
}

private void setupDataBase() {
    myAdap = new DBHelper(getApplicationContext(), "courses", null, 1);
    myAdap.insertCourses();

}

private void setupAdapter()
{
if(myAdap.getCourses()!=null)
    {
    cAdapter adapter = new  cAdapter(this, R.layout.list_courses, myAdap.getCourses());
    list.setAdapter(adapter);
    }

}}

我看过Android: Your content must have a ListView whose id attribute is android.R.id.list或:RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'以及其他问题,但不适合我,这里发生了什么?

非常感谢您的帮助

1 个答案:

答案 0 :(得分:11)

如果您想扩展ListActivity,则应使用

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

您将获得ListView

ListView listview = this.getListView();

ListView listview = (ListView)findViewById(android.R.id.list);
//consider android.R prefix

以下是详细说明:How to use getListView() in Activity?

相关问题