onActivity当我在第二个 Activity 上按 "cancel" 按钮时出现错误

onActivityResult error when I press "cancel" button on my second Activity

我有下一个活动:

Activity1

//declare
private static final int SAVE_DATA_FROM_ACTIVITY = 203;

//........... not important code

//button to open second Activity
public void btn_openSecondActivity(View view)
    {

            Intent intent = new Intent(Activity2.this, Activity1.class);

            startActivityForResult(intent, SAVE_DATA_FROM_ACTIVITY);
        }
    }


 protected void onActivityResult (int requestCode, int resultCode, Intent data)
    {
      if (requestCode == SAVE_DATA_FROM_activity)
        {
            name= data.getStringExtra("Name");
        }
    }


//....... not important code

Activity2

第二个 Activity 我有两个按钮:

  1. 取消
  2. 保存

//......................

//declare
private static final int OK_RESULT_CODE = 1;

//Cancel button
public void btn_cancel(View view)
{
    finish();
}

//Save button
public void btn_save (View view)
{

    Intent intent = new Intent();
    intent.putExtra("Name",et_name.getText().toString());

    setResult(OK_RESULT_CODE, intent);

    finish();
}

问题

当我点击保存按钮时一切正常,但问题是当我点击取消按钮时,它报告了一个错误:

Failure delivering result ResultInfo{who=null, request=203, result=0, data=null} to activity {com.example.alvaro.project/com.alvaro.project.Activity1}: java.lang.NullPointerException

我理解这个问题,当我取消时结果代码不一样但是我不知道如何解决它

有什么建议吗?

您的 onActivityResult 方法有问题。你不检查结果。

将您的条件更改为:

if (requestCode == SAVE_DATA_FROM_activity)

至:

if (resultCode  == OK_RESULT_CODE  && requestCode == SAVE_DATA_FROM_activity)

改变这个

if (requestCode == SAVE_DATA_FROM_activity)
    {
        name= data.getStringExtra("Name");
    }

进入

   if (requestCode == SAVE_DATA_FROM_activity&&resultCode==RESULT_OK)
    {
        name= data.getStringExtra("Name");
    }

你的取消方法就像

public void btn_cancel(View view)
{
   setResult(RESULT_CANCELED);
    finish();
}

而不是 OK_RESULT_CODE 使用 Android 默认 ok 像 Activity.RESULT_OK

检查您是否设法设置结果

protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
  if (requestCode == SAVE_DATA_FROM_activity && resultCode = Activity2.OK_RESULT_CODE)
    {
        name= data.getStringExtra("Name");
    } else {
       //probably btn_cancel pressed
    }
}

检查 onActivityResult 中的 resultCode

 protected void onActivityResult (int requestCode, int resultCode, Intent data)
    {
      if (resultCode == 1/*OK_RESULT_CODE from Second Activity */ && requestCode == SAVE_DATA_FROM_activity)
        {
            name= data.getStringExtra("Name");
        }
    }

活动 1

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == SAVE_DATA_FROM_activity) {
        if (resultCode == Activity.RESULT_OK) {
            name = data.getStringExtra("Name");
        } else if (resultCode == Activity.RESULT_CANCELED){
            // TODO something
        }
    }
}

活动 2

删除字段OK_RESULT_CODE

//Cancel button
public void btn_cancel(View view) {
    setResult(Activity.RESULT_CANCELED, new Intent());
    finish();
}

//Save button
public void btn_save(View view) {
    Intent intent = new Intent();
    intent.putExtra("Name", et_name.getText().toString());
    setResult(Activity.RESULT_OK, intent);

    finish();
}