gpt4 book ai didi

java - onActivityResult() 和 : failure delivering result resultinfo who=null request=1 result=-1 data=intent (has extras)

转载 作者:行者123 更新时间:2023-11-29 20:59:22 25 4
gpt4 key购买 nike

我在一个用数据填充 Intent 的类中得到了这个方法,然后将它传递回启动该方法的 Activity 。在这里,我使用 Intent 将项目添加到 onActivityResult() 中的数据库

但正如标题所示,我遇到了“无法提供结果 resultinfo”错误。

谁能看出我为什么会收到此错误?

这是我的代码的重要部分:

来自第 1 类,接收 Intent :

    @Override
protected void onActivityResult(int reqCode, int resCode, Intent data){
super.onActivityResult(reqCode, resCode, data);

if(reqCode == ENTER_DATA_REQUEST_CODE && resCode == RESULT_OK){
hkDBhelper.addItem(data.getExtras().getString("tag_grocery_foodItem"),
data.getExtras().getString("tag_grocery_weight"),
data.getExtras().getString("tag_grocery_price"),
data.getExtras().getString("tag_grocery_brand"),
data.getExtras().getString("tag_grocery_category"));
hkCursorA.changeCursor(hkDBhelper.listAll());
}
}

编辑:添加:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.addItem) {
startActivityForResult(new Intent(this, AddFood.class), ENTER_DATA_REQUEST_CODE);
return true;
}
return super.onOptionsItemSelected(item);
}

第 2 类,发送 Intnet:

    public void addItem(){
setFood = itemFood.getText().toString();
setWeight = itemWeight.getText().toString();
setPrice = itemPrice.getText().toString();
setBrand = itemBrand.getText().toString();
setCategory = catagorySpinner.getSelectedItem().toString();


if(setFood.length() != 0 && setWeight.length() != 0 && setPrice.length() != 0
&& setBrand.length() != 0 && setCategory.length() != 0){
Intent newIntent = getIntent();
newIntent.putExtra("tag_grocery_foodItem", setFood);
newIntent.putExtra("tag_grocery_weight", setWeight);
newIntent.putExtra("tag_grocery_price", setPrice);
newIntent.putExtra("tag_grocery_brand", setBrand);
newIntent.putExtra("tag_grocery_category", setCategory);

this.setResult(RESULT_OK, newIntent);
finish();
}
}

这是来自 logcat 的一些行:

10-29 09:28:23.569: E/AndroidRuntime(32565): java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=1, result=-1, data=Intent { cmp=com.example.s188094_mappe3/.AddFood
(has extras) }} to activity {com.example.s188094_mappe3/com.example.s188094_mappe3.MainActivity}:
java.lang.NullPointerException

最佳答案

尝试这种方式,希望这能帮助您解决问题。

有两种方式向/从另一个 Activity 发送/获取数据

1.向 Intent 添加数据。

怎么放:

Intent newIntent = new Intent();
newIntent.putExtra("tag_grocery_foodItem", setFood);
newIntent.putExtra("tag_grocery_weight", setWeight);
newIntent.putExtra("tag_grocery_price", setPrice);
newIntent.putExtra("tag_grocery_brand", setBrand);
newIntent.putExtra("tag_grocery_category", setCategory);
setResult(RESULT_OK, newIntent);
finish();

获取方式:

if(reqCode == ENTER_DATA_REQUEST_CODE && resCode == RESULT_OK){
hkDBhelper.addItem(data.getStringExtra("tag_grocery_foodItem"),
data.getStringExtra("tag_grocery_weight"),
data.getStringExtra("tag_grocery_price"),
data.getStringExtra("tag_grocery_brand"),
data.getStringExtra("tag_grocery_category"));
hkCursorA.changeCursor(hkDBhelper.listAll());
}

2.将数据添加到bundle并将bundle添加到intent。

怎么放:

Intent newIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("tag_grocery_foodItem", setFood);
bundle.putString("tag_grocery_weight", setWeight);
bundle.putString("tag_grocery_price", setPrice);
bundle.putString("tag_grocery_brand", setBrand);
bundle.putString("tag_grocery_category", setCategory);
newIntent.putExtras(bundle);
setResult(RESULT_OK, newIntent);
finish();

获取方式:

if(reqCode == ENTER_DATA_REQUEST_CODE && resCode == RESULT_OK){
hkDBhelper.addItem(data.getExtras().getString("tag_grocery_foodItem"),
data.getExtras().getString("tag_grocery_weight"),
data.getExtras().getString("tag_grocery_price"),
data.getExtras().getString("tag_grocery_brand"),
data.getExtras().getString("tag_grocery_category"));
hkCursorA.changeCursor(hkDBhelper.listAll());
}

关于java - onActivityResult() 和 : failure delivering result resultinfo who=null request=1 result=-1 data=intent (has extras),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26625741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com