我正在使用带有FragmentStatePagerAdapter的NaviagtionDrawer。 当我在NavigationDrawer中切换到第二个选项时,PagerAdapter强制关闭。
堆栈跟踪:
10-25 17:25:25.518: W/dalvikvm(1788): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
10-25 17:25:25.538: E/AndroidRuntime(1788): FATAL EXCEPTION: main
10-25 17:25:25.538: E/AndroidRuntime(1788): java.lang.NullPointerException
10-25 17:25:25.538: E/AndroidRuntime(1788): at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:819)
10-25 17:25:25.538: E/AndroidRuntime(1788): at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:152)
10-25 17:25:25.538: E/AndroidRuntime(1788): at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
10-25 17:25:25.538: E/AndroidRuntime(1788): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-25 17:25:25.538: E/AndroidRuntime(1788): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
10-25 17:25:25.538: E/AndroidRuntime(1788): at android.os.Handler.handleCallback(Handler.java:605)
10-25 17:25:25.538: E/AndroidRuntime(1788): at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 17:25:25.538: E/AndroidRuntime(1788): at android.os.Looper.loop(Looper.java:137)
10-25 17:25:25.538: E/AndroidRuntime(1788): at android.app.ActivityThread.main(ActivityThread.java:4441)
10-25 17:25:25.538: E/AndroidRuntime(1788): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 17:25:25.538: E/AndroidRuntime(1788): at java.lang.reflect.Method.invoke(Method.java:511)
10-25 17:25:25.538: E/AndroidRuntime(1788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-25 17:25:25.538: E/AndroidRuntime(1788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-25 17:25:25.538: E/AndroidRuntime(1788): at dalvik.system.NativeStart.main(Native Method)
代码:
private void selectItem(int position) {
// update the main content by replacing fragments
PlanetFragment fragment = new PlanetFragment();
// Bundle args = new Bundle();
// args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
//fragment.setArguments(args);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.myfivepanelpager, fragment);
fragmentTransaction.commit();
// update selected item and t itle, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
NaviagtionDrawer OncreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_pager, container, false);
return rootView;
}
FragmentStatePagerAdapter OncreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_pager, container, false);
final ViewHolder holder = new ViewHolder();
holder.imageView = (ImageView)rootView.
findViewById(R.id.imageView1);
holder.progressBar=(ProgressBar)rootView.findViewById(R.id.progressBar1);
//some code for swiping pages }}