Reputation:
I have a fragment and activity. From fragment I am passing a string named day to activity by putExtra . But getting null pointer exception when I am retrieving in an activity.
Passing a String.
private Intent i;
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
day = "Mon";
i = new Intent(getActivity(), AddEventActivity.class);
i.putExtra("aKey", day);
startActivityForResult(i, 1);
}
});
Retrieving a String :
i = new Intent();
dayOfWeek = i.getStringExtra("aKey");
weekDay.setText(dayOfWeek);
Whats going wrong?
Upvotes: 0
Views: 910
Reputation: 1083
String dayOfWeek = getIntent().getExtras().getString("aKey");
hope this will work.
Upvotes: 2
Reputation: 692
In activity , if you want receive data
i = getIntent();
dayOfWeek = i.getStringExtra("aKey");
weekDay.setText(dayOfWeek);
Upvotes: -1
Reputation: 5220
you are creating a new Intent
so there is no extra in it. you should retrieve passed intent in your Activity
try using
i = getIntent();
instead.
Upvotes: 0
Reputation: 132992
Use getIntent()
in onCreate
method of AddEventActivity
Activity instead of new Intent()
:
i = getIntent();
Also add null
check before accessing any method from i
.
Upvotes: 0