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

7

159

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?

Bravin answered 15/6, 2012 at 12:40 Comment(0)
M
348

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.

Marshall answered 15/6, 2012 at 12:41 Comment(8)
Doing this did fix my problem, thanks. But I wonder if you would be so kind as to write a few words of explanation as to when a ListView must have the ID "@android:id/list" and when it is OK for it to have an arbitrary name. It's because I like to not only fix my problem but also to understand why the fix was needed.Truckle
Works. But why this 'games' with id depending on the view impl?Bisexual
When I add @android, Eclipse says list cannot be resolved or is not a fieldSmalley
same problem it shows error: Error: No resource found that matches the given name (at 'id' with value '@android:id/list_interestsent').Wireman
@Truckle I believe this is because in the @android namespace android keeps many constants so it can implement classes like ListActivity which need to know which component is your ListView if you're using a custom layout.Studied
I have replaced to "MainActivity extends Activity" solved my issue. thanksTownes
@id/android:list also seems to work, any thoughts on that?Grass
Wondering the difference between android:id="@+id/list" and android:id="@android:id/list" Could it be possible that the latter one is referred to an existing android is called list? But what's that?Monocyte
R
23

You should have one listview in your mainlist.xml file with id as @android:id/list

<ListView
    android:id="@android:id/list"
    android:layout_height="wrap_content"
    android:layout_height="fill_parent"/>
Rebuff answered 15/6, 2012 at 12:41 Comment(0)
F
15
<ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

this should solve your problem

Featherstone answered 15/6, 2012 at 12:54 Comment(0)
B
8

Exact way I fixed this based on feedback above since I couldn't get it to work at first:

activity_main.xml:

<?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="@android:id/list"
>
</ListView>

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addPreferencesFromResource(R.xml.preferences);

preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
    android:key="upgradecategory"
    android:title="Upgrade" >
    <Preference
        android:key="download"
        android:title="Get OnCall Pager Pro"
        android:summary="Touch to download the Pro Version!" />
</PreferenceCategory>
</PreferenceScreen>
Burnisher answered 9/1, 2014 at 5:15 Comment(0)
W
4

Inherit Activity Class instead of ListActivity you can resolve this problem.

public class ExampleActivity extends Activity  {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.mainlist);
    }
}
Winona answered 22/11, 2014 at 10:13 Comment(0)
D
1
<ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical"/>
Dementia answered 6/2, 2018 at 4:17 Comment(0)
M
0

One other thing that affected me: If you have multiple test devices, make sure you are making changes to the layout used by the device. In my case, I spent a while making changes to xmls in the "layout" directory until I discovered that my larger phone (which I switched to halfway through testing) was using xmls in the "layout-sw360dp" directory. Grrr!

Manganate answered 13/3, 2016 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.