我开发了一个app,其中包含5个(一般)片段。
在主要活动中,我通过按下一个按钮(主活动中的5个按钮)替换片段,每个片段加载到mainActivity中的FrameLayout。
当我按1-2-3-4按钮时 - 没关系,碎片会更换更多时间.. 但是,当我按下5个按钮更多时间 - 应用程序崩溃与NPE例外。
按更多时间意味着 - 我点击按钮5,然后点击另一个按钮,然后再次点击5按钮 - 崩溃。
mainActivity中的代码5按钮:
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.appFragmentContainer, fragmentMyAccount);
fragmentTransaction.commit();
片段代码,按下5按钮时加载: 它是片段,其中我加载了3个片段的页面适配器。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
片段类:
public class Fragment_my_account extends Fragment {
ViewPager mViewPager;
TabPagerAdapter mTabPagerAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_my_account, container,false);
mViewPager = (ViewPager) v.findViewById(R.id.pager);
mTabPagerAdapter = new TabPagerAdapter(getChildFragmentManager());
mViewPager.setAdapter(mTabPagerAdapter);
return v;
}
}
页面适配器代码:
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
//Fragement for panic btn settings tab
return new Fragment_a();
case 1:
//Fragment for tracking settings
return new Fragment_b();
case 2:
//Fragment for general settings
return new Fragment_c();
}
return null;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
我想,当我按下按钮5时 - 加载三个片段(三个中的一个),我可以从左到右改变动作手指。
有谁知道,我做错了什么?
非常感谢!
编辑1: 例外
05-01 16:42:00.113 1492-1492/my.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:585)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1286)
at android.view.View.dispatchRestoreInstanceState(View.java:12093)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2582)
at android.view.View.restoreHierarchyState(View.java:12071)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:465)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:979)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)