So I am trying to get a ListView to be populated underneath a button on the same layout. Basically when a ListView option is selected the button will respond differently to each selected option. The issue is it is getting a Force close and it seems to be a problem with populating the ListView.
<?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"
android:background="@drawable/background"
>
<Button
android:background="@drawable/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_gravity="center"
android:id="@+id/button"/>
<ListView
android:listSelector="#990000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:id="@+id/listview"
/>
</LinearLayout>
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.listview);
ArrayList<String> list = new ArrayList<String>();
soundsList.add("one");
soundsList.add("two");
soundsList.add("three");
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(arrayAdapter);
The Logcat readout is this:
08-23 10:21:23.735: ERROR/AndroidRuntime(588): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
I have tried exactly what it says in the ArrayAdapter… (android.R.id.list. I’ve tried numerous other things including an extends ListActivity, but regardless of what I try I seem to get a similar Logcat response.
Is there a better way to pull off what I am trying to accomplish? I want the button above the ListView all on the same main.xml.
Thanks in advance.
EDIT:
I had also tried this code. Currently commented out, but it was what I tried originally.
setListAdapter(new ArrayAdapter<String>(this, android.R.id.list,LIST));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
LIST is a Strings array. The output on Logcat from the above code was the same as the previous.
Sounds like you are either trying to implement a ListActivity or a ListFragment, which expects to find a listview instance with a specific name, hence the error. Try changing the id of your listview from:
to:
I know that you said that you already tried this, but given your code, it looks like its still wrong. Once you change it can you post the new error message? I realize according to your description that it’s going to be a similar message, but even if the difference is slight it’s probably still significant.
EDIT:
Another thing I just noticed – if you are indeed using ListActivity or FragmentActivity, get a ref to the ListView by calling
getListView() instead offindViewById()`.