I am trying to pass an integer from a fragment to another java class however I keep getting an error: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String, int)' on a null object reference
Bundle b = new Bundle();
b.putInt("id", position);
Intent passInt = new Intent(view.getContext(), displayAccount.class);
passInt.putExtras(b);
startActivity(passInt);
startActivity(new Intent(view.getContext(), displayAccount.class));
Then in displayAccount.java:
int id = (getIntent().getExtras().getInt("id",0));
You called double times startActivity
. It will be work when you delete this line startActivity(new Intent(view.getContext(), displayAccount.class));
. Because you didn't put bundle on this line, you just starting the activity. You already give intent with startActivity(passInt);
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.