显示“片段获取”后,应用将永久冻结

时间:2018-12-04 16:29:01

标签: android android-fragments android-bottomappbar

自从我实现了BottomAppBar以来,即使我没有做任何更改,当应显示某个片段时,我的应用也没有响应。也没有错误消息,单击BottomNavigationBar中的另一个项目后,所有显示的其他片段都可以正常显示。

片段:

public class StatisticsFragment extends Fragment {

private TabLayout mTabLayout;
private ViewPager mViewPager;

private BankAccountsStatisticsFragment bankAccountsStatisticsFragment = new BankAccountsStatisticsFragment();
private BillsStatisticsFragment billsStatisticsFragment = new BillsStatisticsFragment();
private CategoriesStatisticsFragment categoriesStatisticsFragment = new CategoriesStatisticsFragment();
private GoalsStatisticsFragment goalsStatisticsFragment = new GoalsStatisticsFragment();


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

    View inflatedView = inflater.inflate(R.layout.fragment_statistics, container, false);

    mTabLayout = (TabLayout) inflatedView.findViewById(R.id.tl_statistics);
    mViewPager = (ViewPager) inflatedView.findViewById(R.id.vp_statistics);

    mViewPager.setAdapter(new Adapter(getChildFragmentManager()));
    mTabLayout.setupWithViewPager(mViewPager);

    return inflatedView;
}

private class Adapter extends FragmentPagerAdapter {

    private static final int TABS = 4;

    public Adapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0: return bankAccountsStatisticsFragment;
            case 1: return billsStatisticsFragment;
            case 2: return categoriesStatisticsFragment;
            case 3: return goalsStatisticsFragment;
            default: throw new IllegalStateException("Couldn't find a fragment for position " + position);
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0: return getString(R.string.tab_bank_accounts);
            case 1: return getString(R.string.tab_bills);
            case 2: return getString(R.string.tab_categories);
            default: return getString(R.string.tab_goals);
        }
    }

    @Override
    public int getCount() {
        return TABS;
    }
}

}

LOGCAT

LOGCAT

1 个答案:

答案 0 :(得分:0)

扩大布局并创建Fragment过渡(例如“ FragmentManager .... replace()。commit()”)是在呈现UserInterface的MainThread中完成的,因此UserInterface在这些时刻将冻结。 我认为您必须预先加载“坏的”片段,然后隐藏它而不是销毁它...这样,下次您要创建它时就已经准备好了。