Tablayout,应用程序在切换标签时崩溃 - 错误膨胀类片段

时间:2016-07-29 19:21:17

标签: android android-fragments android-viewpager android-fragmentactivity android-tablayout

尝试切换回标签后,我收到以下错误。

07-29 15:32:53.337 28041-28041/com.bestworkouts.sheikoworkout E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.bestworkouts.sheikoworkout, PID: 28041
                                                                            android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class fragment
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
                                                                                at com.bestworkouts.sheikoworkout.WorkoutDetailFragment.onCreateView(WorkoutDetailFragment.java:34)
                                                                                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                                at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1430)
                                                                                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:732)
                                                                                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                                at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                                at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                                at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
                                                                                at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
                                                                                at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
                                                                                at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2008)
                                                                                at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025)
                                                                                at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995)
                                                                                at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272)
                                                                                at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377)
                                                                                at android.view.View$PerformClick.run(View.java:22526)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:158)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                             Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
                                                                                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
                                                                                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
                                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
                                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
                                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                                                                                at com.bestworkouts.sheikoworkout.WorkoutDetailFragment.onCreateView(WorkoutDetailFragment.java:34) 
                                                                                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
                                                                                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
                                                                                at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1430) 
                                                                                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:732) 
                                                                                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
                                                                                at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
                                                                                at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:1177) 
                                                                                at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608) 
                                                                                at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570) 
                                                                                at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551) 
                                                                                at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2008) 
                                                                                at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025) 
                                                                                at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995) 
                                                                                at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272) 
                                                                                at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377) 
                                                                                at android.view.View$PerformClick.run(View.java:22526) 
                                                                                at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                at android.os.Looper.loop(Looper.java:158) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                             Caused by: java.lang.IllegalArgumentException: Binary XML file line #18: Duplicate id 0x7f0d006d, tag null, or parent id 0x7f0d006a with another fragment for com.bestworkouts.sheikoworkout.NavigationDrawerFragment
                                                                                at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)
                                                                                at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                                at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:192)
                                                                                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:758)
                                                                                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
                                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 
                                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
                                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:855) 
                                                                                at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:527) 
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                                                                                at com.bestworkouts.sheikoworkout.WorkoutDetailFragment.onCreateView(WorkoutDetailFragment.java:34) 
                                                                                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
                                                                                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
                                                                                at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1430) 
                                                                                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:732) 
                                                                                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
                                                                                at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
                                                                                at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:1177) 
                                                                                at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608) 
                                                                                at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570) 
                                                                                at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551) 
                                                                                at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2008) 
                                                                                at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025) 
                                                                                at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995) 
                                                                                at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272) 
                                                                                at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377) 
                                                                                at android.view.View$PerformClick.run(View.java:22526) 
                                                                                at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                at android.os.Looper.loop(Looper.java:158) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

该应用程序打开正常,我可以正常切换到每个标签,但是当尝试切换回已经打开的标签时,它会崩溃。我已经读过尝试更改它以扩展FragmentActivity而不是Fragment它可能会修复它,但是当我这样做时,我的onCreateView方法不起作用。

这是我的片段类:

 public class WorkoutDetailFragment extends Fragment {

public WorkoutDetailFragment() {
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

    RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
    rv.setHasFixedSize(true);
    RvAdapter adapter = new RvAdapter(new String[] {"test 1", "test 2", "test 3", "test 4", "test 5"});
    rv.setAdapter(adapter);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    rv.setLayoutManager(llm);

    return rootView;
}
}

这是设置tablayout的类。虽然我不相信错误在那里,但我可能是错的。

public class WorkoutDaysActivity extends BaseActivity{

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workout_days);
    mToolBar = activateToolbar();
    setUpNavigationDrawer();

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    PagerAdapter pagerAdapter =
            new PagerAdapter(getSupportFragmentManager(), WorkoutDaysActivity.this);

    viewPager.setAdapter(pagerAdapter);

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    // Iterate over all tabs and set the custom view
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        tab.setCustomView(pagerAdapter.getTabView(i));
    }

}


@Override
public void onResume() {
    super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class PagerAdapter extends FragmentPagerAdapter {

    String tabTitles[] = new String[] { "Monday", "Wednesday", "Friday" };
    Context context;

    public PagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new WorkoutDetailFragment();
            case 1:
                return new WorkoutDetailFragment();
            case 2:
                return new WorkoutDetailFragment();
        }

        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

    public View getTabView(int position) {
        View tab = LayoutInflater.from(WorkoutDaysActivity.this).inflate(R.layout.custom_tab, null);
        TextView tv = (TextView) tab.findViewById(R.id.custom_text);
        tv.setText(tabTitles[position]);
        return tab;
    }

}}

感谢您的帮助!

0 个答案:

没有答案