-1

i am trying to load the list of items from arrays... i get runtime error

02-22 11:22:51.042: E/AndroidRuntime(1460): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

my R.java file

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
        public static final int icon=0x7f020001;
    }
    public static final class id {
        public static final int mylist=0x7f050000;
        public static final int next=0x7f050005;
        public static final int play=0x7f050004;
        public static final int prev=0x7f050003;
        public static final int seekbar=0x7f050002;
        public static final int selectedfile=0x7f050001;
        public static final int text1=0x7f050006;
    }
    public static final class layout {
        public static final int audiolist=0x7f030000;
        public static final int song_item=0x7f030001;
        public static final int songlist=0x7f030002;
    }
    public static final class string {
        public static final int app_name=0x7f040000;
    }
}

my code:

@Override
public void onCreate(Bundle icicle) {
    try {
        super.onCreate(icicle);
      setContentView(R.layout.audiolist);

        ListView listView = (ListView) findViewById(R.id.mylist);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile"};
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listView.setAdapter(spinnerArrayAdapter);

    } catch (NullPointerException e) {
        Log.v(getString(R.string.app_name), e.getMessage());
    }
}

layout: audiolist

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

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:id="@+id/mylist"
        android:layout_weight="1.0"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/screen_background_light"
        android:padding="10dip">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/selectedfile"
            android:text="Not file selected"
            android:textColor="@android:color/black"
            android:gravity="center_horizontal"
            android:singleLine="true"
            android:ellipsize="middle"/>

        <SeekBar
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/seekbar"
            android:max="100"
            android:paddingBottom="10dip"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@android:drawable/screen_background_light">

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/prev"
                android:src="@android:drawable/ic_media_previous"/>

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/play"
                android:src="@android:drawable/ic_media_play"/>

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/next"
                android:src="@android:drawable/ic_media_next"/>

        </LinearLayout>
    </LinearLayout>

</LinearLayout>
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • 2
    You should look at that : http://stackoverflow.com/questions/3040374/runtime-exception-listview-whose-id-attribute-is-android-r-id-list – Jeremy D Feb 22 '12 at 16:32

2 Answers2

1

From the reference

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

You ll either have to change the listview's id to that default or you need to use setContentView(...)

Update: Code sample

You can check the tutorial of Vogella, it s a great one showing u step by step. You cannot fail.

Update 2 Possible solution My last guess is that your activity extends ListActivity so the builder is looking for a default list as the layout. IF so, use simple Activity instead and see the answer to this question: How can I implement a ListView without ListActivity? (use only Activity)

Good luck

Community
  • 1
  • 1
Orkun
  • 6,998
  • 8
  • 56
  • 103
  • in your reference its populating the ListView from Contacts but i have my own Array that i want to populate from, as i have in my above code... any help? – Nick Kahn Feb 22 '12 at 18:26
  • 1) no it doesnT list from contacts. 2) where ure filling from is irrelevant. But i see u are confused so i ll update my code. If you want more answers, I suggest u update your question with the latest state of XML and code. – Orkun Feb 22 '12 at 18:32
  • Thanks Zortkun, yes i think i am lost :),.....so let me ask you before i jump into the notepad tutorial,.... in order to fix my code what correction do i need? i want to start with very simple like what i am trying to do is to just load arraylist in my listview but not sure what is causing to crash my app... – Nick Kahn Feb 22 '12 at 18:38
-1

When you are extending ListActivity and creating listView, id of ListView should be just "list" not mylist.

<ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:id="@+id/list"
        android:layout_weight="1.0"/>
kosa
  • 65,990
  • 13
  • 130
  • 167
  • i changed to `list` but still getting the error: 02-22 ` E/AndroidRuntime(1530): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' ` – Nick Kahn Feb 22 '12 at 17:06
  • Should be android.R.id.list not your.package.R.id.list ... So this answer is wrong .. Read first comment this is good one – Selvin Feb 22 '12 at 17:36
  • @Selvin: i tried `ListView listView = (ListView) findViewById(android.R.id.list);` still get the same error – Nick Kahn Feb 22 '12 at 18:09