There was a bug when passing data between the activity, but there was no error in finding it.
Record both the code and the log, and later study
Code:
Mainactivity.class
1 PackageCom.example.caradviser;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Importandroid.app.Activity;7 Importandroid.content.Intent;8 ImportAndroid.net.Uri;9 ImportAndroid.os.Bundle;Ten ImportAndroid.util.Log; One ImportAndroid.view.View; A ImportAndroid.view.Window; - ImportAndroid.widget.Spinner; - ImportAndroid.widget.TextView; the ImportAndroid.widget.Toast; - - Public classMainactivityextendsActivity { - + Private Static FinalString TAG = "Com.example.caradviser"; - TextView Showbacktext; + A @Override at protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - requestwindowfeature (window.feature_no_title); - Setcontentview (r.layout.activity_main); -showbacktext=(TextView) Findviewbyid (r.id.backmainactivity); - } in - @Override to protected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) { + Super. Onactivityresult (Requestcode, ResultCode, intent); - the Switch(requestcode) { * Case123123: $ if(resultcode==RESULT_OK) {Panax Notoginseng if(intent==NULL||"". Equals (Intent)) { -Toast.maketext (mainactivity. This, "The data obtained is empty", Toast.length_short). Show (); the}Else { +String getData = Intent.getstringextra ("Data_return"); AShowbacktext.settext (Intent.getstringextra ("Data_return")); the } + } - Break; $ $ default: - Break; - } the } - Wuyi the Public voidToAnotherActivity3 (View v) { -String data = "Hello, this is the data passed"; WuIntent Intent =NewIntent (mainactivity. This, Threeactivity.class); -Intent.putextra ("haha", data); AboutStartactivityforresult (Intent, 123123); $ } -}
Threeactivity.class
1 PackageCom.example.caradviser;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 Importandroid.text.TextUtils;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.view.Window;Ten ImportAndroid.widget.Button; One ImportAndroid.widget.TextView; A ImportAndroid.widget.Toast; - - Public classThreeactivityextendsActivity { the - PrivateButton backtomainactivity; - PrivateString data; - PrivateTextView TextView; + - @Override + protected voidonCreate (Bundle savedinstancestate) { A //TODO auto-generated Method Stub at Super. OnCreate (savedinstancestate); - requestwindowfeature (window.feature_no_title); - Setcontentview (r.layout.three_layout); - -Intent Intent =getintent (); -data = Intent.getstringextra ("haha"); in -TextView =(TextView) Findviewbyid (r.id.showtext); to + if(Textutils.isempty (data)) { -Toast.maketext (threeactivity. This, "No data received from other activity", Toast.length_short). Show (); the}Else { * textview.settext (data); $ }Panax Notoginseng - theBacktomainactivity =(Button) Findviewbyid (r.id.backmainactivity); +Backtomainactivity.setonclicklistener (NewOnclicklistener () { A the @Override + Public voidOnClick (View v) { - //TODO auto-generated Method Stub $Intent Intent =NewIntent (); $String data3= "will return to Mainactivity"; -Intent.putextra ("Data_return", data3); - Setresult (result_ok,intent); the finish (); - }Wuyi }); the } - Wu}
Log
110-12 16:22:02.304:e/androidruntime (16265): FATAL exception:main210-12 16:22:02.304:e/androidruntime (16265): Process:com.example.caradviser, pid:16265310-12 16:22:02.304:e/androidruntime (16265): java.lang.RuntimeException:Failure delivering result resultinfo{who=NULL, request=123123, Result=-1, data=intent {(have extras)} to activity {com.example.caradviser/com.example.caradviser.MainActivity}: Java.lang.NullPointerException410-12 16:22:02.304:e/androidruntime (16265): at Android.app.ActivityThread.deliverResults (activitythread.java:3375)510-12 16:22:02.304:e/androidruntime (16265): at Android.app.ActivityThread.handleSendResult (Activitythread.java : 3418)610-12 16:22:02.304:e/androidruntime (16265): at android.app.activitythread.access$1300 (ActivityThread.java:141)710-12 16:22:02.304:e/androidruntime (16265): at Android.app.activitythread$h.handlemessage (ActivityThread.java : 1261)810-12 16:22:02.304:e/androidruntime (16265): at Android.os.Handler.dispatchMessage (handler.java:102)910-12 16:22:02.304:e/androidruntime (16265): at Android.os.Looper.loop (looper.java:136)Ten10-12 16:22:02.304:e/androidruntime (16265): at Android.app.ActivityThread.main (activitythread.java:5072) One10-12 16:22:02.304:e/androidruntime (16265): At Java.lang.reflect.Method.invokeNative (Native Method) A10-12 16:22:02.304:e/androidruntime (16265): at Java.lang.reflect.Method.invoke (method.java:515) -10-12 16:22:02.304:e/androidruntime (16265): at Com.android.internal.os.zygoteinit$methodandargscaller.run ( zygoteinit.java:793) -10-12 16:22:02.304:e/androidruntime (16265): at Com.android.internal.os.ZygoteInit.main (zygoteinit.java:609) the10-12 16:22:02.304:e/androidruntime (16265): At Dalvik.system.NativeStart.main (Native Method) -10-12 16:22:02.304:e/androidruntime (16265): caused By:java.lang.NullPointerException -10-12 16:22:02.304:e/androidruntime (16265): at Com.example.caradviser.MainActivity.onActivityResult ( Mainactivity.java:42) -10-12 16:22:02.304:e/androidruntime (16265): at Android.app.Activity.dispatchActivityResult (activity.java:5441) +10-12 16:22:02.304:e/androidruntime (16265): at Android.app.ActivityThread.deliverResults (activitythread.java:3371) -10-12 16:22:02.304:e/androidruntime (16265): ... One more
Specific what causes, at present I am not sure, wait for the ability to improve, look back to this question.
An unresolved bug in Android development