类扩展ListActivity"其ID属性为“android.R.id.list'"

问题描述:

我想延长我的主要活动为ListActivity(previously延伸至:活动)使用onListItemClick,但logcat的,给我你的内容必须具备的一个ListView其id属性为机器人.r.id.list'。 (我使用SQLite,让填充ListView控件)。

I'm trying to extend my main Activity to "ListActivity" (previously extends to: Activity) to use onListItemClick, but the logcat, send me "Your content must-have a ListView whose id attribute is 'android.r.id.list'". (i'm using SQLite for populate the 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:

this is the complete xml for the ListView:

<?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:

My main class to handle the 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:您的内容必须有一个ListView的id属性是android.R.id.list ,或者:runtime例外的ListView的id属性是'android.R.id.list和其他问题,但不要对我的作品,这是怎么回事呢?

I read Android: Your content must have a ListView whose id attribute is android.R.id.list, or: runtime exception ListView whose id attribute is 'android.R.id.list', and other questions, but don't works for me, what's going on here?

真的希望AP preciate你的帮助

really would appreciate your help

当你想扩展 ListActivity 你应该使用

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

,你会得到你的的ListView

ListView listview = this.getListView();

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

下面是详细的解释:如何在活动中使用getListView()