Skip to content Skip to sidebar Skip to footer

Bundle In Fragment Is Null

I am trying to send data from MainActivity to SunFragment (the main fragment) as: MainActivity public class MainActivity extends AppCompatActivity { private FusedLocationProvid

Solution 1:

 mainActivity.java
 ============

  SquardFragment   
          squardFragment=SquardFragment.newInstance(matchId2,page,matchStatus);
       FragmentManagerfragmentManager= getSupportFragmentManager();
    FragmentTransactiontransaction= fragmentManager.beginTransaction();
    transaction.replace(R.id.container, squardFragment, mTag);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    if (canAddtoBackStack) transaction.addToBackStack(mTag);
    transaction.commit();


  SquardFragment.java
  =================== 

   publicstatic SquardFragment newInstance(String matchId,
        int page,String matchStatus)    {
    SquardFragmentfrag=newSquardFragment();
    BundleargsBundle=newBundle();
    argsBundle.putString("matchId", matchId);
    argsBundle.putInt("page", page);
    argsBundle.putString("matchStatus", matchStatus);
    frag.setArguments(argsBundle);
    return frag;
  }

  if (getArguments() != null) {
        matchId = getArguments().getString("matchId");
        page = getArguments().getInt("page", 0);
        matchStatus=getArguments().getString("matchStatus");
    }

Solution 2:

The problem is you pass a new instance of your SunFragment to fragmentManager.beginTransaction().replace() instead of using the instance you created with the arguments set.

FragmentSunFragment=newSunFragment();
SunFragment.setArguments(bundle);

//this line is the problem
fragmentManager.beginTransaction().replace(R.id.view_pager, newSunFragment()).commit(); 

//change it to this
fragmentManager.beginTransaction().replace(R.id.view_pager, SunFragment).commit(); 

Solution 3:

The problem is in this line,

fragmentManager.beginTransaction().replace(R.id.view_pager, new SunFragment()).commit();

you are giving a new object of Sunfragment each time

simply put sunFragment instead of new sunFragment.

Post a Comment for "Bundle In Fragment Is Null"