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");
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.