Search code examples
javaandroidandroid-fragmentsnullpointerexceptionbundle

Fragment getArguments() Null Pointer Exception


Almost new to the Android world, I'm having an issue passing parameters between fragments. I need it to set the id of a particular tab of a tab navigation menu.

In my MainActivity.java I'm creating a new instance of my TabFragment and then starting the transaction like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    mFragmentManager = getSupportFragmentManager();
    ...
    Fragment fragment = TabFragment.newInstance(0);
    fragmentTransaction(mFragmentManager, fragment);
    ...
}

where

private void fragmentTransaction(FragmentManager mFragmentManager, Fragment fragment) {
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.containerView, fragment).commit();
}

(I incapsulated it in a method 'cause I need that in other places, too.)

In my TabFragment.java I've written the usual newInstance() method like this:

public static TabFragment newInstance(int position) {
    Log.d("POSITION", "newInstance: " + position);
    TabFragment fragment = new TabFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_POSITION, position);

    fragment.setArguments(args);

    return fragment;
}

The problem is that, staying in TabFragment.java, my getArguments() call is giving back an empty pointer, 'cause it looks like my savedInstanceState is empty too.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    int position;
    try {
        position = savedInstanceState.getInt(ARG_POSITION);
    } catch (NullPointerException e) {
        position = -1;
    }
    Log.d("BUNDLE", "position: " + position);

    int position = getArguments().getInt(ARG_POSITION);

    ...
}

The code is crashing at getArguments(). Commenting that line, I discovered through the exception-catch that the Bundle is empty (position = -1).

Any hint about what I'm making wrong? I've looked around for similar cases, but I can't apply those solutions to my code. Thank you for any help.


Solution

  • I think that I may have fixed it.

    I just put

    position = getArguments().getInt(ARG_POSITION);
    

    inside of my try-catch instead of using

    position = savedInstanceState.getInt(ARG_POSITION);
    

    Now my onCreateView() looks like this, and everything is working.

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        int position;
    
        try {
            position = getArguments().getInt(ARG_POSITION);
        } catch (NullPointerException e) {
            position = -1;
        }
    
        Log.d("BUNDLE", "position: " + position);
    
        ...
    }
    

    Hope it will help whoever having my same problem. Fragments are not as easy as it may look like.