0

我在片段中显示 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)
4

3 回答 3

4

改变这个

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

到,

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

问题出在身份证上。当您扩展ListFragment时,您的 ListView 必须具有类似这样的 id,@android:id/list

于 2012-09-06T07:21:18.877 回答
3

而不是android:id="@+id/listview1"使用android:id="android:id/list"

因为,ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)

于 2012-09-06T07:24:08.943 回答
1

您已经进行了子类化ListFragment,并且这已经托管了一个ListView对象。您的视图层次结构必须包含一个带有 id 的 ListView 对象,@android:id/list而不是@+id/listview1

于 2012-09-06T07:27:09.327 回答