0

フラグメント内に ListView を表示する際に問題があります。Fragment で 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;
  }
}

カテゴリ_フラグメント.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"/>

問題はIDにあります。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ます。ビュー階層には、@android:id/list代わりにid を持つ ListView オブジェクトが含まれている必要があります。@+id/listview1

于 2012-09-06T07:27:09.327 に答える