我添加了一个新活动并将其设置为导航类型中的标签滑动。到目前为止一切顺利,然后当我滑动到另一个选项卡时,它崩溃并给我这个错误(但它至少向我显示了布局):
01-11 15:01:45.342: D/AndroidRuntime(29447): Shutting down VM
01-11 15:01:45.342: W/dalvikvm(29447): threadid=1: thread exiting with uncaught exception (group=0x4208a930)
01-11 15:01:45.350: E/AndroidRuntime(29447): FATAL EXCEPTION: main
01-11 15:01:45.350: E/AndroidRuntime(29447): java.lang.NullPointerException
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:352)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:347)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:692)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.support.v4.view.ViewPager.populate(ViewPager.java:875)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1539)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1422)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.View.getDisplayList(View.java:12633)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.View.getDisplayList(View.java:12689)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.View.getDisplayList(View.java:12583)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.View.getDisplayList(View.java:12689)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.View.getDisplayList(View.java:12583)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.View.getDisplayList(View.java:12689)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.View.getDisplayList(View.java:12583)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.View.getDisplayList(View.java:12689)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1198)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2173)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.Choreographer.doFrame(Choreographer.java:532)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.os.Handler.handleCallback(Handler.java:725)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.os.Looper.loop(Looper.java:137)
01-11 15:01:45.350: E/AndroidRuntime(29447): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-11 15:01:45.350: E/AndroidRuntime(29447): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 15:01:45.350: E/AndroidRuntime(29447): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 15:01:45.350: E/AndroidRuntime(29447): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-11 15:01:45.350: E/AndroidRuntime(29447): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-11 15:01:45.350: E/AndroidRuntime(29447): at dalvik.system.NativeStart.main(Native Method)
切换标签:
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AddExpense();
break;
case 1:
fragment = new AddIncomeSource();
break;
default:
break;
}
return fragment;
}
选项卡的内部类(每个选项卡的每个类):
public static class AddExpense extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public AddExpense() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_expense, null);
return v;
}
}
public static class AddIncomeSource extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public AddIncomeSource() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_income, null);
return v;
}
}