Inexplicable ListView


#1

I have found these forums quite helpful, hopefully my situation will help others.

As mentioned before, Android Studio now defaults activities to android.support.v7.app.ActionBarActivity. The benefits of this are many as it cleans up some of the compatibility code required in this chapter, etc.

With this in mind, my SingleFragmentActivity and CrimePagerActivity both implement ActionBarActivity. After this change, I receive an exception when creating a CrimeFragment:

java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344) at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:961) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486) at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163) at android.support.v4.view.ViewPager.populate(ViewPager.java:1073) at android.support.v4.view.ViewPager.populate(ViewPager.java:919) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)

To solve the problem, I added to fragment_crime.xml:

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

This seems rather silly. When the view is inflated, the list is empty; it has no effect on the view at all. Unfortunately the addition of the ListView is the only way I can avoid the aforementioned runtime error. I’m hoping someone with more experience than I, can explain to me the reasoning for this.


#2

Does your CrimeFragment extend ListFragment? It should be just a normal Fragment. ListFragment requires you to have a list in the layout you inflate IIRC.


#3

That did the trick, thanks.