Android开发之onActivityResult()中的resultCode为0,intent为null的解决办法

BUG:昨天在使用activity之间传值的时候,遇到了一个bug,该bug为:Activity A启动Activity B,然后在Activity B中取到一个值,并通过back键返回到Activity A,但是在onActivityResult()中,发现requestCode正确,resultCode为0,intent为null。

log:

 1 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=198007, result=0, data=null} to activity {com.example.xxx.testactivity/com.example.xxx.testactivity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString()' on a null object reference
 2                                                                                      at android.app.ActivityThread.deliverResults(ActivityThread.java:4058)
 3                                                                                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:4101)
 4                                                                                      at android.app.ActivityThread.access$1400(ActivityThread.java:177)
 5                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1497)
 6                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
 7                                                                                      at android.os.Looper.loop(Looper.java:145)
 8                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5942)
 9                                                                                      at java.lang.reflect.Method.invoke(Native Method)
10                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
11                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
12                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
13                                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString()' on a null object reference
14                                                                                      at com.example.lijingbo.testactivity.MainActivity.onActivityResult(MainActivity.java:37)
15                                                                                      at android.app.Activity.dispatchActivityResult(Activity.java:6549)
16                                                                                      at android.app.ActivityThread.deliverResults(ActivityThread.java:4054)
17                                                                                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:4101) 
18                                                                                      at android.app.ActivityThread.access$1400(ActivityThread.java:177) 
19                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1497) 
20                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
21                                                                                      at android.os.Looper.loop(Looper.java:145) 
22                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5942) 
23                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
24                                                                                      at java.lang.reflect.Method.invoke(Method.java:372) 
25                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
26                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

查看到: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=198807, result=0, data=null}

返回的结果码和数据都不对,只有请求码正确。

经过尝试后,发现,复写onBackPressed()方法的时候,把自己写的代码放在super.onBackPressed()上面,就可以正确的执行。

放在super.onBackPressed()的下面,虽然debug的时候也执行了,但是并没有把值传递给Activity A。

解决办法:复写onBackPressed()的时候,把自己的代码放在super.onBackPressed()上面。

posted @ 2016-03-07 15:41  熠然  阅读(12754)  评论(1编辑  收藏  举报