Reputation: 4279
I created a new activity with navigation type "tabs + swipe". I made these changes in the getItem() method :
@Override
public Fragment getItem(int i) {
Fragment fragment ;
switch(i){
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 3:
fragment = new Fragment3();
break;
//
default : return null;
}
//set args if necessary
//return fragment
return fragment;
}
so for each tab I've defined a separate class. One of the class is this :
public static class Fragment1 extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public Fragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
View v = inflater.inflate(R.layout.center, null);
return v;
}
}
when I run the app the new activity starts successfully but when I swipe the screen to the second tab then the app crashes... this is the logcat :
02-16 08:48:43.328: E/AndroidRuntime(21541): FATAL EXCEPTION: main
02-16 08:48:43.328: E/AndroidRuntime(21541): java.lang.NullPointerException
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.view.ViewPager.populate(ViewPager.java:992)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.view.ViewPager$3.run(ViewPager.java:237)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.os.Handler.handleCallback(Handler.java:605)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.os.Looper.loop(Looper.java:137)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.app.ActivityThread.main(ActivityThread.java:4575)
02-16 08:48:43.328: E/AndroidRuntime(21541): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 08:48:43.328: E/AndroidRuntime(21541): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 08:48:43.328: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-16 08:48:43.328: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
02-16 08:48:43.328: E/AndroidRuntime(21541): at dalvik.system.NativeStart.main(Native Method)
Please solve my problem.
Upvotes: 1
Views: 2765
Reputation: 5409
Most likely, your default statement returning null and it tries to add a null fragment.. and fails... do you know that you are missing case 2 for your switch statement.. is it on purpose?
If I were you, I would put a break
switch(i){
and check the value of i... and see if its either, 0, 1 or 3... otherwise your app will crash becuz it is returning null.
Upvotes: 6