简体   繁体   中英

java.lang.NullPointerException: (Fragment)

I'm trying to create a Navigation Menu + Tabs, the code for this is fine.

Now I've implemented some code I had as an Activity in a Fragment in order to add it to the Tabs viewer.

When I run the code the app works just fine .. Until I swipe right to the other Tab and then the App crashed and I get this from the logcat :

java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference

I'm leaving you my Main Activity, 'cause I think there's the issue, if you think you might need some Fragments like the Tabs, please just ask!

CODE: http://pastebin.com/TTcJMR77

You have to add default case on your switch in getItem(int position) method:

default:
return new Fragment();

Instead of

return null;

Like this:

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new YourFragment();
        default:
            return new Fragment();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM