1

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

2
  • 2
    Please include the relevant code in the question. Commented Oct 23, 2015 at 18:23
  • Did you see the Pastebin? @Matsemann Commented Oct 23, 2015 at 18:54

1 Answer 1

1

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();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Just to know, are you from Italy?
I think I need to change all my imports on the support.v4 on the simple .app one right?
Yes, if you want, we can move discussion to chat.
Might be a better option for everyone yeah
But I think ya can start it, not me
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.