FET
FET

Reputation: 942

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

Upvotes: 1

Views: 118

Answers (1)

Michele Lacorte
Michele Lacorte

Reputation: 5363

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();
    }
}

Upvotes: 1

Related Questions