0

I try to pass values to my fragment from my Activity but I getting NullPointerException error. this is the code

Activity.java

String name = "checkout";
Fragment myFragment= new Fragment();
Bundle bundle = new Bundle();
bundle.putString("name", name);
myFragment.setArguments(bundle);

Fragment.java

assert getArguments() != null;
String name = getArguments().getString("name");

1 Answers1

0

I use another way to create fragment.

On activity :

String name = "checkout";
Bundle bundle = new Bundle();
bundle.putString("name", name);

MyFragment.newInstance(bundle)

On Fragment :

fun newInstance(bundle: Bundle?): MyFragment? {
        if (bundle == null)
            return null

        val fragment = MyFragment()
        fragment.mUserId = bundle.getString("name")
        return fragment
    }

Tell me if it's good

Rémy
  • 313
  • 3
  • 17