Davide3i
Davide3i

Reputation: 1053

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.

Upvotes: 4

Views: 8666

Answers (4)

Rakshit Nawani
Rakshit Nawani

Reputation: 2604

What I have done is shown below

Call your fragment like this

     getSupportFragmentManager().beginTransaction().replace(R.id.main_container, MyMeetings.newInstance("**YOUR DATA**"), "TAG OF FRAGMENT").commit();

R.id.main_container is my Frame Layout

Now inside your Fragment do this

  public static MyMeetings newInstance(String data) {
        MyMeetings fragment = new MyMeetings();


        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        fragment.setArguments(bundle);
        return fragment;
    }

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String data = getArguments().getString("data");

    }

String data contains your passed data

Upvotes: 0

Anuj Sharma
Anuj Sharma

Reputation: 4324

Send data with Fragment

    try {
         Bundle bundle = new Bundle();
         bundle.putString("key","value");    // data that you want to send
         LoginFragment fragment = new LoginFragment(); // Fragment that you want to call
         fragment.setArguments(bundle);
         FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
         fts.replace(R.id.fragmentHolder, fragment);
         fts.addToBackStack(fragment.getClass().getSimpleName());
         fts.commit();
     }
    catch (Exception e){
        e.printStackTrace();
   }

Receive data with Bundle in other Fragment

try {
      Bundle bundle = new Bundle();
      String data = bundle.getString("key");  // Receive data through key
    }
     catch (Exception e){
            e.printStackTrace();
        }

Upvotes: 2

Davide3i
Davide3i

Reputation: 1053

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.

Upvotes: 0

ishmaelMakitla
ishmaelMakitla

Reputation: 3812

Try calling the getArguments in the onActivityCreated instead. Your code should be something like this:

public void onActivityCreated(Bundle savedInstanceState) {  
 super.onActivityCreated(savedInstanceState);

 Bundle arguments = getArguments();
 int position = arguments.getInt(ARG_POSITION);
 ...
}

I hope this helps you. Give this a try and let me know if it helps.

Upvotes: 0

Related Questions