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?

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Isa Kuru
  • 2,079
  • 4
  • 16
  • 12

7 Answers7

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.

GAMA
  • 5,958
  • 14
  • 79
  • 126
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • 4
    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. – RenniePet Jul 29 '13 at 15:33
  • 4
    Works. But why this 'games' with id depending on the view impl? – ses Oct 14 '13 at 04:22
  • 1
    When I add `@android`, Eclipse says `list cannot be resolved or is not a field` – user83039 Nov 13 '14 at 04:24
  • 1
    same problem it shows error: Error: No resource found that matches the given name (at 'id' with value '@android:id/list_interestsent'). –  Nov 19 '14 at 12:21
  • @RenniePet 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. – Jpaji Rajnish Dec 16 '15 at 23:31
  • I have replaced to "MainActivity extends Activity" solved my issue. thanks – Vineesh TP Feb 09 '16 at 06:26
  • @id/android:list also seems to work, any thoughts on that? – Selçuk Cihan Mar 07 '16 at 15:06
  • 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? – Alston Sep 05 '19 at 08:10
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"/>
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
15
<ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

this should solve your problem

ndeverge
  • 21,378
  • 4
  • 56
  • 85
Aamirkhan
  • 5,746
  • 10
  • 47
  • 74
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>
Kevin
  • 2,296
  • 21
  • 22
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);
    }
}
Vicky
  • 5,098
  • 2
  • 33
  • 31
1
<ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical"/>
Makvin
  • 3,475
  • 27
  • 26
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!

RudyF
  • 805
  • 1
  • 10
  • 16