-1

Hi I am working on populating a ListView with an arraylist in Android, after clicking on a button to move from the first screen to the second. But after clicking on the first screen the following error appears. Any help is really appreciated.

second_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listDisplayData"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

SecondActivity.java

package com.example.listexample;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SecondActivity extends Activity {

ListView listDisplayData;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        //reference to listview
        ListView listDisplayData = (ListView)findViewById(R.id.listDisplayData);

        final ArrayList<String> arr = new ArrayList<String>();
        arr.add("name");
        arr.add("address");

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.list_content, arr);

        listDisplayData.setAdapter(adapter);
 };
}

Error:

01-13 16:11:37.028: E/AndroidRuntime(1188): FATAL EXCEPTION: main
01-13 16:11:37.028: E/AndroidRuntime(1188): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listexample/com.example.listexample.DisplayData}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.os.Looper.loop(Looper.java:137)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at java.lang.reflect.Method.invokeNative(Native Method)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at java.lang.reflect.Method.invoke(Method.java:511)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at dalvik.system.NativeStart.main(Native Method)
01-13 16:11:37.028: E/AndroidRuntime(1188): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.Activity.setContentView(Activity.java:1881)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at com.example.listexample.DisplayData.onCreate(DisplayData.java:26)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.Activity.performCreate(Activity.java:5104)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-13 16:11:37.028: E/AndroidRuntime(1188):     ... 11 more

If any more detail is required then please let me know.

  • what don't you understand ? `Your content must have a ListView whose id attribute is 'android.R.id.list'` – njzk2 Jan 13 '14 at 17:12
  • also, please read your error. it is on DisplayData, not on SecondActivity... – njzk2 Jan 13 '14 at 17:12
  • You have another activity which extends ListView but doesn't have android.R.id.list as id ! Your secondActivity is fine – Adnan Mulla Jan 13 '14 at 17:16

2 Answers2

1

The Exception is telling you that your ListView needs to have android.R.id.list as an id. This is typically happens if you are creating your own layout for a ListActivity or ListFragment. Both of these require that you have a ListView with that id so you can make use of the convenience methods provided by each of these classes among other things.

The Exception is thrown by the DisplayData class not SecondActivity. I am quite sure that you are using either a ListFragment or ListActivity there.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
0

I think the error was the one simply shown in the error message.

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

Rename the id of your ListView like this,

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

Check this answer for more information

Community
  • 1
  • 1
albusshin
  • 3,930
  • 3
  • 29
  • 57