Fragment中的ListView给出了错误

时间:2012-09-06 07:17:51

标签: android

我在片段中显示ListView时遇到问题。我在片段中使用ListFragment。我无法弄清楚我的实际问题在哪里。

CategoriesFragment.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)

3 个答案:

答案 0 :(得分: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"/>

to,

<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"/>

问题在于ID。当你扩展 ListFragment 时,你的ListView必须具有这样的id,@android:id/list

答案 1 :(得分: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)

答案 2 :(得分:1)

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

的ListView对象