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>
From the reference
You ll either have to change the
listview's id to that default or you need to usesetContentView(...)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
ListActivityso 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