I try to implement a simple list / detailed view, as shown in Figure 1 here , but I get “Your content should have a ListView whose id attribute is“ android.R.id.list. ”This error seems to have been discussed in detail if the number of results hasn’t changed, but most of the answers seem to be related to the ListActivity extension, while I am expanding the ListFragment. So far I have not been able to fix this.
SpeciesListActivity.java
public class SpeciesListActivity extends MenuItemActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_species_list);
Fragments are included through the layout file:
activity_species_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:baselineAligned="false" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.fragments.SpeciesListFragment" android:id="@+id/species_list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="net.gamebreeder.fragments.SpeciesDetailFragment" android:id="@+id/species_detail" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
This is a layout of two panels, one window is the same, except that the second fragment (id species_detail) is not included.
This snippet:
SpeciesListFragment.java
public class SpeciesListFragment extends ListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
And the fragment layout:
fragment_species_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="8dp" android:paddingRight="8dp"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:drawSelectorOnTop="false"/> <TextView android:id="@id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/empty_string"/> </LinearLayout>
I tried changing the SpeciesListFragment so as not to override onCreateView () to force it to use the default values, since according to docs :
Note. If your fragment is a subclass of ListFragment, the default implementation returns a ListView from onCreateView (), so you don't need to implement it.
But I still get: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
One question I found suggested that I extend FragmentActivity in the main action, and exactly what MenuItemActivity is:
public class MenuItemActivity extends FragmentActivity {
This is basically a shell, so I don’t need to manually create a menu in every action.
Any help would be appreciated - I hope I just missed something simple.
Edit 2013-06-27
I tried formatting the id in fragment_species_list.xml as @id/android:list and @android:id/list , but it doesn’t work. The docs say use @id/android:list , but the other questions I read seem to suggest that both should work, but I get an error with both. I also tried @+id/list .
I also changed the following based on the answers below:
activity_species_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:baselineAligned="false" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class="com.example.fragments.SpeciesListFragment" android:id="@+id/species_list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment class="net.gamebreeder.fragments.SpeciesDetailFragment" android:id="@+id/species_detail" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>