0

Scenario is :

I Have a MainActivity(MainActivity.java) with pager making Fragment(MyFragment.java) in it.

I have one more activity called Main2Activity(Main2activity.java).

I want to send Data from Main2Activity to MyFragment.

Main2Activity.java:

  public class Main2Activity extends AppCompatActivity{

  private String myString = "hello";

  @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main2);

            Bundle bundle = new Bundle();
            bundle.putString("path1", "Hey Brother");

            MyFragment frgmentObj = new MyFragment ();
            frgmentObj.setArguments(bundle);

   }

MyFragment.java :

   public class MyFragment extends android.support.v4.app.Fragment{

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

String brother = getArguments().getString("path1");   // Here showing NPE
  return view;
}
}

But its giving me Null pointer exception

     java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

please help me out package is same Thanks

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
G Setia
  • 61
  • 6

1 Answers1

0

Try this way.

Bundle bundle = this.getArguments();
String brother = bundle.getString("path1");

Hope this will help.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50