Android error: Your content must have a ListView whose id attribute is ''

Posted on May 31, 2022

Learn how you can resolve the error with ListView requiring id attribute

When you create a ListActivity class to host a ListView object, Android may complain about the lack of the id attribute for that ListView.

For example, suppose you have a MainActivity class as follows:

public class MainActivity extends ListActivity {

    protected void onCreate(Bundle savedInstanceState) {

Then, you create the following activity_main.xml layout:

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

When you build and run the Android application, a RuntimeException error will occur with the following message:

E/AndroidRuntime: FATAL EXCEPTION: main
# ...
Caused by: java.lang.RuntimeException: 
Your content must have a ListView whose id attribute is ''
    at #...

The exception above happens because the ListActivity class specifically requires a ListView object that has an id attribute of @android:id/list.

The value @+id/list resolves to while @android:id/list resolves to

To resolve the error, you need to change the android:id attribute in your ListView widget as shown below:

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

Rebuild your application and run it again. This time, the error should disappear.

Also, please note that the ListActivity class has been deprecated in API level 30 (Android 11)

When you need to display a UI component with a list of items, you are recommended to use either ListFragment or RecyclerView instead.

Now you’ve learned how to resolve the missing id attribute in ListActivity class. Good work! 👍

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.