0

我已经开始了另一个活动,我想将结果返回给调用它的活动,我已经阅读了一些教程并尝试了它,但是我遇到了一些这样的错误:

08-23 17:48:58.005: E/AndroidRuntime(581): java.lang.RuntimeException: Failure delivering result ResultInfo{who=Tab2, request=0, result=-1, data=Intent { cmp=com.organizer.medical.activities/.AddPatientActivity (has extras) }} to activity {com.organizer.medical.activities/com.organizer.medical.activities.Main}: java.lang.NullPointerException
08-23 17:48:58.005: E/AndroidRuntime(581):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
08-23 17:48:58.005: E/AndroidRuntime(581):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
08-23 17:48:58.005: E/AndroidRuntime(581):  at android.app.ActivityThread.access$1100(ActivityThread.java:123)
08-23 17:48:58.005: E/AndroidRuntime(581):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
08-23 17:48:58.005: E/AndroidRuntime(581):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 17:48:58.005: E/AndroidRuntime(581):  at android.os.Looper.loop(Looper.java:137)
08-23 17:48:58.005: E/AndroidRuntime(581):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-23 17:48:58.005: E/AndroidRuntime(581):  at java.lang.reflect.Method.invokeNative(Native Method)
08-23 17:48:58.005: E/AndroidRuntime(581):  at java.lang.reflect.Method.invoke(Method.java:511)
08-23 17:48:58.005: E/AndroidRuntime(581):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-23 17:48:58.005: E/AndroidRuntime(581):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-23 17:48:58.005: E/AndroidRuntime(581):  at dalvik.system.NativeStart.main(Native Method)
08-23 17:48:58.005: E/AndroidRuntime(581): Caused by: java.lang.NullPointerException
08-23 17:48:58.005: E/AndroidRuntime(581):  at com.organizer.medical.activities.PatientActivity.onActivityResult(PatientActivity.java:347)
08-23 17:48:58.005: E/AndroidRuntime(581):  at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:122)
08-23 17:48:58.005: E/AndroidRuntime(581):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
08-23 17:48:58.005: E/AndroidRuntime(581):  ... 11 more

这是我的调用活动代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
      if (resultCode == RESULT_OK) {
        int cat = Integer.parseInt(getIntent().getExtras().get("category").toString());
        loadList(cat);
       }
 }
}

这是在我的通话活动中的 oncreate() 上:

startActivityForResult(new Intent(this, CalledActivity.class), ADD_PATIENT);

这是我的“被调用”活动代码:

   setResult(RESULT_OK, getIntent().putExtra("category", status.isChecked()?1:2));
   finish();

顺便说一句,在我的被调用活动上,setResult()并且finish()只能onClick通过该被调用活动上的按钮完成......

我真的不知道如何正确使用startActivityForResult(). 请分享您对此的了解。

更新


事实证明,我在 onActivityResult() 上访问的意图导致了 null 异常错误,结果证明传递给该方法的“日期”变量就是这样使用的:

int cat = Integer.parseInt(**data**.getExtras().get("category").toString());
4

2 回答 2

1

您好,此示例返回一个字符串,但对于示例,它会很好:

Intent intent = new Intent();
Bundle b = new Bundle();
b.putString("lastPath", this.myPath.getText().toString());
intent.putExtras(b);
setResult(SUCCESS_RETURN_CODE, intent);
finish();

要读取值,请使用:

Bundle b = data.getExtras();
lastPath = b.getString("lastPath");
于 2012-08-23T10:28:13.733 回答
0

事实证明,我在 onActivityResult() 上访问的意图导致了 null 异常错误,结果证明传递给该方法的“日期”变量就是这样使用的:

int cat = Integer.parseInt( data .getExtras().get("category").toString());

于 2012-08-23T11:03:54.217 回答