I am trying to get the result displayed as toast but the app crashes at this line Plant plant = (Plant) data.getSerializableExtra(PlantResultActivity.PLANT_RESULT);
PlantResultActivity:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
// get the item that the user clicked
Plant plant = (Plant) getListAdapter().getItem(position);
// EveryThing went fine
getIntent().putExtra(PLANT_RESULT, plant);
// Finish this Intent
finish();
}
AdvancedSearchActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
//are we getting data returend from the plantResultIntent? If so, this if test will evaluate to true, because
//we passed the PLANT_RESULTS constant in when invoked that intent
if(requestCode == PLANT_RESULTS){
//fetch the selected data using the constant that we have using as a key
Plant plant = (Plant) data.getSerializableExtra(PlantResultActivity.PLANT_RESULT);
//This Toast will be invoked if we recieved a result from plantResultIntent
Toast.makeText(this, "Recieved Result " + plant, Toast.LENGTH_LONG).show();
}
}
you missed the setResult
call, and the returned intent, the one you use in the onActivityResult
, is null
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
// get the item that the user clicked
Plant plant = (Plant) getListAdapter().getItem(position);
Intent intent = new Intent();
// EveryThing went fine
intent.putExtra(PLANT_RESULT, plant);
setResult(RESULT_OK, intent);
// Finish this Intent
finish();
}
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.