views:

277

answers:

2

Newb alert, I'm sure I'm doing something dumb here.

I've been progressively expanding my UI, and I want to add a ListView in the middle of my UI. When I add it and change the activity to extend a ListActivity instead of just an Activity, I'm getting a Force Close. Using 1.5. Does a ListView not work embedded in a RelativeLayout?

Thanks

public class Categories extends ListActivity{

final static String[] ITEMS = {"blah", "floop", "gnarlp", "stuff"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.categories);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.listrow, R.id.textview, ITEMS);
        setListAdapter(adapter);

XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"&gt;
 <ImageView 
  android:id="@+id/ImageView01" 
  android:layout_height="fill_parent" android:layout_width="fill_parent" android:scaleType="center" android:background="@drawable/background">
 </ImageView>
 <ImageView android:id="@+id/ImageView02" android:src="@drawable/cat_heading" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerHorizontal="true">
  </ImageView>
<ListView android:id="@+id/ListView01" android:layout_below="@id/ImageView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView>
<RelativeLayout android:id="@+id/RelativeLayout02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignBottom="@+id/RelativeLayout01" android:layout_centerHorizontal="true">
<ImageButton android:id="@+id/ImageButtonRecipes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:src="@drawable/recipes"></ImageButton>
<ImageButton android:id="@+id/ImageButtonSearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/ImageButtonRecipes" android:src="@drawable/search"></ImageButton>
</RelativeLayout>
</RelativeLayout>

and the listrow.xml:

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

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview"/>
</LinearLayout>
A: 

I'm pretty sure a ListActivity needs the setContentView parameter to be a ListView Id. Try extending just a regular Activity instead.

Jon M
A: 

A ListActivity must have a ListView with the ID "@id/android:list", change your ID. It works in a RelativeLayout too.

See Listactivity for further info.

molnarm
Thanks. Duh to me, I'd seen that on other posts and when I added the list through the Layout editor in Eclipse it gave it the default name ListView01. Thanks, it works now!
Tom