Fragment中的ViewPager返回IllegalStateException

时间:2017-08-15 17:39:49

标签: java android android-fragments android-viewpager

我想在页面片段中创建带有PagerView的选项卡,但运行以下代码会返回IllegalStateException:

源代码:

public class HomeFragment extends Fragment {

private TabLayout tabs;
private ClassesFragment classesFragment;
private ExamsFragment examsFragment;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    classesFragment = new ClassesFragment();
    examsFragment = new ExamsFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_main_classes, container, false);
    Toolbar toolbar = getActivity().findViewById(R.id.toolbar);

    ViewPager viewPager = new ViewPager(getContext());
    viewPager.setLayoutParams(new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    setupViewPager(viewPager);
    toolbar.addView(viewPager);

    tabs = new TabLayout(getContext());
    tabs.setLayoutParams(new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    toolbar.addView(tabs);
    tabs.setupWithViewPager(viewPager);
    return v;
}

// Add Fragments to Tabs
private void setupViewPager(ViewPager viewPager) {
    PagerAdapter adapter = new PagerAdapter(getChildFragmentManager());
    adapter.addFragment("Занятия", ExamsFragment.newInstance());
    viewPager.setAdapter(adapter);
}

private class PagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();

    public PagerAdapter(FragmentManager fm) {
        super(fm);
        fragments.add(ExamsFragment.newInstance());
        titles.add("new");
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    public void addFragment(String title, Fragment fragment)
    {
        titles.add(title);
        fragments.add(fragment);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}

IllegalStateException:

  

处理:com.company.user.blabla,PID:22256                                                                                 java.lang.IllegalStateException:带适配器的ViewPager com.company.user.blabla.fragments.HomeFragment$PagerAdapter@ca3cef6需要一个视图id                                                                                     在android.support.v4.app.FragmentPagerAdapter.startUpdate(FragmentPagerAdapter.java:81)                                                                                     在android.support.v4.view.ViewPager.populate(ViewPager.java:1119)                                                                                     在android.support.v4.view.ViewPager.populate(ViewPager.java:1087)                                                                                     在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1613)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.support.v7.widget.Toolbar.measureChildCollapseMargins(Toolbar.java:1547)                                                                                     在android.support.v7.widget.Toolbar.onMeasure(Toolbar.java:1655)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)                                                                                     在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)                                                                                     在android.widget.LinearLayout.measureVertical(LinearLayout.java:806)                                                                                     在android.widget.LinearLayout.onMeasure(LinearLayout.java:685)                                                                                     在android.support.design.widget.AppBarLayout.onMeasure(AppBarLayout.java:223)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)                                                                                     在android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:713)                                                                                     在android.support.design.widget.AppBarLayout $ Behavior.onMeasureChild(AppBarLayout.java:1040)                                                                                     在android.support.design.widget.AppBarLayout $ Behavior.onMeasureChild(AppBarLayout.java:770)                                                                                     在android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:783)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1080)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)                                                                                     在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                                     在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)                                                                                     在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)                                                                                     在android.widget.LinearLayout.measureVertical(LinearLayout.java:806)                                                                                     在android.widget.LinearLayout.onMeasure(LinearLayout.java:685)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)                                                                                     在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)                                                                                     在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)                                                                                     在android.widget.LinearLayout.measureVertical(LinearLayout.java:806)                                                                                     在android.widget.LinearLayout.onMeasure(LinearLayout.java:685)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)                                                                                     在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                                     在com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)                                                                                     在android.view.View.measure(View.java:21998)                                                                                     在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)                                                                                     在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)                                                                                     在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)                                                                                     在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)                                                                                     在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6733)                                                                                     在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:911)                                                                                     在android.view.Choreographer.doCallbacks(Choreographer.java:723)                                                                                     在android.view.Choreographer.doFrame(Choreographer.java:658)                                                                                     在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:897)                                                                                     在android.os.Handler.handleCallback(Handler.java:789)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:98)                                                                                     在android.os.Looper.loop(Looper.java:164)

我对android有很多经验。搜索无用或我搜索不正确。我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

您的PagerAdapter流程不正确..请点击此链接

https://guides.codepath.com/android/ViewPager-with-FragmentPagerAdapter

希望这会对你有帮助..