Asked  7 Months ago    Answers:  2   Viewed   37 times

I have created an xml file like this:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list" >
</ListView>

and an activity:

public class ExampleActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainlist);
    }
}

As you see, I have not done anything else. But I'am getting the error:

Your content must have a ListView whose id attribute is 'android.R.id.list'

Even though I have the android:id="@+id/list" line in my xml.

What is the problem?

 Answers

56

Rename the id of your ListView like this,

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

Since you are using ListActivity your xml file must specify the keyword android while mentioning to a ID.

If you need a custom ListView then instead of Extending a ListActivity, you have to simply extend an Activity and should have the same id without the keyword android.

Tuesday, June 1, 2021
 
wavyGravy
answered 7 Months ago
60

Your problem is you are trying to extends ListActivity and you don't have a ListView with

android:id="android.R.id.list"

You need to have a ListView in your xml with that id. Please add it to your xml

You can tell by the following line in your logcat

Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

If you find the next line that references your Activity it will tell you the class and line number

at testing.android.application.three.MainActivityNext.onCreate(MainActivityNext.java:28)

So MainActivityNext line 28 is showing the exception.

Note

As stated in a comment, please don't just post unformatted logcat when asking for help. Please try to look for where the error is occurring and post relevant code along with a description of what the problem is. This one was easy to see from the error but usually we will need to see some code

You can use CTRL+K to format your code and logcat or use the coding brackets above {}

Friday, August 6, 2021
 
yinka
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share