onActivityResult() and: failure delivering result

2019-04-16 03:12发布

问题:

I've got this method in one class filling an Intent with data and then pass it back to the activity that started the method. Here I am using the intent to add the item to my database in onActivityResult()

But as the title indicates I'm getting an "failure delivering result resultinfo" error.

Can anyone see why I get this error?

Heres the essential part of my code:

from class 1, receiving the 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());
    }
}

Edit: added:

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

And class 2, sending the 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();
    }
}

And heres some lines from 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

回答1:

Try this way,hope this will help you to solve your problem.

There is two way send/get data to/from another activty

1.add data to intent.

how to put :

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

how to get :

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.Add data to bundle and add bundle to intent.

how to put :

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

how to get :

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


回答2:

Your problem lies here:

activity {com.example.s188094_mappe3/com.example.s188094_mappe3.MainActivity}

Your package appears twice. It tries to open

com.example.s188094_mappe3/com.example.s188094_mappe3.MainActivity.class 

but that doesn't exist. Try cleaning your project, reopen Eclipse, all the good stuff.

And before you say anything i don't have enough reputation to comment.

If anyone has a better solution to this please replay.

Cheers!