class extends ListActivity “whose id attribute is 'android.R.id.list'”

ⅰ亾dé卋堺 提交于 2019-12-22 15:00:18

问题


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

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

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>

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);
    }

}}

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

really would appreciate your help


回答1:


When you want to extend ListActivity you should use

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

and you will get your ListView as

ListView listview = this.getListView();

or

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

Here's the detailed explanation: How to use getListView() in Activity?



来源:https://stackoverflow.com/questions/9348544/class-extends-listactivity-whose-id-attribute-is-android-r-id-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!