我已经开始了另一个活动,我想将结果返回给调用它的活动,我已经阅读了一些教程并尝试了它,但是我遇到了一些这样的错误:
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());