ListFragment provides a ListView with id attribute 'android.R.id.list'

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); // Check if the species_detail view is available. If it is // we're running both fragments together. if (findViewById(R.id.species_detail) != null) { } } } 

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) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_species_list, container, false); } } 

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> 
+4
source share
6 answers

I'm not sure what exactly caused this, but I managed to solve it by deleting all the old fragment, activity and layout files that were originally created using the Eclipse ADT template.

I basically rewrote everything from scratch, one part at a time, so that everything gets called correctly. I suspect that this was simply due to the fact that all the links were not updated in the compiled code, despite clearing all the compiled code many times (both manually and using the clean eclipse project tool).

+2
source

Add this to your list:

 <include layout="@android:layout/list_content" /> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" /> 

It worked for me ....

Android documentation: http://developer.android.com/reference/android/app/ListFragment.html

public View onCreateView (inflatable LayoutInflater, ViewGroup container, Bundle savedInstanceState)

Added to API level 11. Provide a default implementation to return a simple list view. Subclasses can override their own layout. If you do this , the returned view hierarchy should have a ListView whose identifier is android.R.id.list , and if desired, may have the form of the id id android.R.id.empty, which should be displayed when the list is empty.

If you override this method to their own content, consider the inclusion of a standard layout list_content the layout file so that you continue to keep the default behavior ListFragment. In particular, this is currently the only way to show the built-in uncertain state of progress.

+7
source

Your fragment_species_list.xml file should be as follows. you have "@ android / id: list" should be "@ id / android: list"

 <?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="@id/android: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> 
-1
source

just replace this xml file

 <?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> 
-1
source

Perhaps this is due to the fact that you are using

 <ListView android:id="@android:id/list" 

try to give

  <ListView android:id="@+id/list" 
-2
source

check ur fragment_species_list.xml ur there

  <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:drawSelectorOnTop="false"/> 

in ur listview, id should be android: id = "@ + id / list"

-3
source

Source: https://habr.com/ru/post/1488351/


All Articles