1

I have searched the forum for the similar issue and as far as I can tell I am doing it the way other solutions suggest, but I still get the error "your content must have a listview whose id attribute is android.r.id.list"

My MainActivity.java looks like:

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
          .add(R.id.container, new PlaceholderFragment()).commit();
    }

    setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1,
        new ArrayList<String>()));  
}

My activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dk.beatpro.mibfinder.MainActivity"
tools:ignore="MergeRootFrame" />

And fragment_mail.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="dk.beatpro.mibfinder.MainActivity$PlaceholderFragment" >

    <Button 
        android:id="@+id/button_findMIB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_findMIB" />

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

</RelativeLayout>

But I get the following runtime error:

06-24 09:00:07.726: E/AndroidRuntime(10542): FATAL EXCEPTION: main
06-24 09:00:07.726: E/AndroidRuntime(10542): Process: dk.beatpro.mibfinder, PID: 10542
06-24 09:00:07.726: E/AndroidRuntime(10542): java.lang.RuntimeException: Unable to start activity ComponentInfo{dk.beatpro.mibfinder/dk.beatpro.mibfinder.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Even though I have android:id="@android:id/list" for the <ListView\> element.

Can anyone tell me what I'm missing here?

Jon Helt-Hansen
  • 383
  • 1
  • 5
  • 19

2 Answers2

1

Your listView with id = "@android:id/list" must be part of activity, not a fragment. But in your code your list view is part of fragment. You can use common activity instead of listActivity and ListFragment (as parent of your PlaceholderFragment class). Or you can move your list from your fragment to your activity.

Alexander Mikhaylov
  • 1,790
  • 1
  • 13
  • 23
1

Your listview resides in fragment_main.xml,but you are writting setContentView(R.layout.activity_main); on your MainActivity.So move your Listview to the appropiate xml file.

kgandroid
  • 5,507
  • 5
  • 39
  • 69