我在片段中显示 ListView 时遇到问题。我在我的片段中使用 ListFragment。我无法弄清楚我的实际问题在哪里。
分类Fragment.java
public class CategoriesFragment extends ListFragment {
ListView lv;
TextView tv;
public CategoriesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(container==null)
return null;
inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.categories_fragment,container,false);
View listView = (ListView)view.findViewById(R.id.listview1);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final String[] fruits = new String[] { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit"};
setListAdapter(new ArrayAdapter<String>
(getActivity(),R.layout.categories_fragment,fruits));
}
}
PagerAdaper.java
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch(i) {
case 0:
Fragment fragment = new CategoriesFragment();
return fragment;
break;
case 1:
Fragment fragment = new FeaturedFragment();
return fragment;
break
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "CATEGORIES";
case 1: return "FEATURED";
}
return null;
}
}
categories_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
DDMS 错误文本:
09-06 12:37:47.402: E/AndroidRuntime(2611): FATAL EXCEPTION: main
09-06 12:37:47.402: E/AndroidRuntime(2611): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.View.measure(View.java:12723)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1017)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.View.measure(View.java:12723)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.View.measure(View.java:12723)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.View.measure(View.java:12723)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)