我有一个活动,里面有 3 个片段.我需要通过单击按钮重新启动我的活动中的第一个片段.
I have an activity that have 3 fragment inside. I need restart a first fragment in my activity with click on Button.
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new HomeFragment(), "Home");
adapter.addFrag(new CategoryFragment(), "Category");
adapter.addFrag(new FilterFragment(), "Filter");
viewPager.setAdapter(adapter);
我测试了以下方式,但不起作用并显示错误:在重新加载时刷新片段
I test the following way but doesn't work and show error: refresh fragment at reload
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setNextAnim(int)' on a null object reference
你可以用这个重新加载你的片段:
You can reload your fragment with this:
FragmentTransaction tr = getFragmentManager().beginTransaction();
tr.replace(R.id.fragment_layout, instanceFragment);
tr.commit()
作为 instanceFragment
您要重新加载的片段实例和 fragment_layout
在您的活动布局 XML 文件中的 FrameLayout
组件.
Being instanceFragment
the instance of the fragment you wanna to reload and fragment_layout
the FrameLayout
component in your activity layout XML file.
这篇关于如何在我的 Activity 中重新启动 Fragment - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!