java - 结果传递失败 ResultInfo(who=null, request=1, result=0, data=null)

标签 java android

我试图将结果显示为 toast ,但应用程序在此行崩溃 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();
}

高级搜索 Activity :

@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();


    }
}

enter image description here

最佳答案

您错过了 setResult 调用,返回的 Intent (您在 onActivityResult 中使用的 Intent )为空

@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();
}

关于java - 结果传递失败 ResultInfo(who=null, request=1, result=0, data=null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28064925/

相关文章:

java - 未捕获的 RuntimeException 和 finally 子句 : which comes first?

java - Hibernate 和序列化

Spring REST 应用程序中安全约束的 Java 配置

android - 如何在MapFragment中创建Tab和PageView?

java - ClassNotFoundException,怎么办?

java - Android - 在运行时从数组添加多个 fragment

java - 将空格转换为命令行类型空格

java - 如何多次绘制相同的运动图像?

java - 我应该如何使用 USB 调试来调试图像捕获 Intent

市场更新后,Android Launcher 应用程序图标未更新为新图标