Reputation: 389
I'm trying to use startActivityForResult() to get a String from another activity, but I keep getting a NullPointerException whenever I try to retrieve the String from the Intent. Here's what I've got:
//To set up the Intent:
String in = mEditText.getText().toString(); //medittext is EditText that I want String from
Intent i = new Intent(this, ActivityLoaderActivity.class); //activity that started this one
i.putExtra("message", in);
setResult(1);
this.finish(); //cause I'm using startActivityForResult()
//To get the String
@Override
protected void onActivityResult( ... , Intent data) {
String s = data.getStringExtra("message"); //error here
}
I know the error is at getStringExtra() through debugging, but I still can't figure out why it's crashing. Anyone have any ideas?
Upvotes: 0
Views: 53
Reputation: 60
Maybe you could try using setResult(1, i)
and checking in onActivityResult()
for the resultCode before getting the extra like this
if(resultCode == 1)
{
//get String extra
}
Hope it helps
Upvotes: 2