使用 StartActivityForResult 和 onResultReceive 我收到错误。有什么问题吗?
给出结果的活动:
public void onClick(View arg0) {
if (Login.length()==0 || Senha.length() == 0) {
Toast.makeText(getLogin.this, "Por favor preencha login e senha!", Toast.LENGTH_LONG);
return;
}
Bundle gGuy = new Bundle();
gGuy.putStringArray("Login", new String[] {Login.getText().toString(), Senha.getText().toString()});
//gGuy.putString("Login", Login.getText().toString());
//gGuy.putString("Senha", Senha.getText().toString());
Intent i = new Intent();
i.putExtra("bGuy", gGuy);
setResult(RESULT_OK, i);
finish();
}
结果接收者:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 555) {
if (resultCode == RESULT_OK) {
Bundle bGuy = data.getExtras();
Info = bGuy.getStringArray("Login");
doMe = new Syncro();
doMe.execute();
}
else {
doLog.doWarning("Erro ao obter login da intent result!");
return;
}
}
}
Logcat 错误:
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): FATAL EXCEPTION: main
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=555, result=-1, data=Intent { (has extras) }} to activity {com.ibssistemas.pda/com.ibssistemas.pda.doSync}: java.lang.NullPointerException
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.os.Looper.loop(Looper.java:123)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at dalvik.system.NativeStart.main(Native Method)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): Caused by: java.lang.NullPointerException
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at com.ibssistemas.pda.doSync$Syncro.onPreExecute(doSync.java:79)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at com.ibssistemas.pda.doSync.onActivityResult(doSync.java:126)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): ... 11 more
已解决: 将 'putExtra
' 更改为 'putExtras
' 结果: i.putExtras(gGuy);
The activity that gives the result:
public void onClick(View arg0) {
if (Login.length()==0 || Senha.length() == 0) {
Toast.makeText(getLogin.this, "Por favor preencha login e senha!", Toast.LENGTH_LONG);
return;
}
Bundle gGuy = new Bundle();
gGuy.putStringArray("Login", new String[] {Login.getText().toString(), Senha.getText().toString()});
//gGuy.putString("Login", Login.getText().toString());
//gGuy.putString("Senha", Senha.getText().toString());
Intent i = new Intent();
i.putExtra("bGuy", gGuy);
setResult(RESULT_OK, i);
finish();
}
And the result receiver:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 555) {
if (resultCode == RESULT_OK) {
Bundle bGuy = data.getExtras();
Info = bGuy.getStringArray("Login");
doMe = new Syncro();
doMe.execute();
}
else {
doLog.doWarning("Erro ao obter login da intent result!");
return;
}
}
}
Logcat error:
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): FATAL EXCEPTION: main
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=555, result=-1, data=Intent { (has extras) }} to activity {com.ibssistemas.pda/com.ibssistemas.pda.doSync}: java.lang.NullPointerException
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.os.Looper.loop(Looper.java:123)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at dalvik.system.NativeStart.main(Native Method)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): Caused by: java.lang.NullPointerException
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at com.ibssistemas.pda.doSync$Syncro.onPreExecute(doSync.java:79)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at com.ibssistemas.pda.doSync.onActivityResult(doSync.java:126)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
08-10 15:13:30.038: ERROR/AndroidRuntime(5489): ... 11 more
SOLVED: Changed 'putExtra
' to 'putExtras
'
Result:i.putExtras(gGuy);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中的这一行有问题:
var 没有名称。
但您的问题可能来自于您在整理活动中使用捆绑包的方式。
尝试
然后你可以按照你已经做的方式在 OnActivityResult 中恢复你的数组(请添加 @Override 注释)。
请阅读有关 java 中的命名约定的内容。
问候,
斯特凡
You have a problem at this line in your code :
var has no name.
But your problem might come from the way you use your bundle in the finishing activity.
Try
Then you can get your array back in OnActivityResult (please add @Override annotation) in the way you already did it.
And please, read this about naming conventions in java.
Regards,
sTéphane
Info = bGuy.getStringArray("Login");
这是错误的行。正确实例化它。
类似于:
String[] Info = bGuy.getStringArray("Login");
Info = bGuy.getStringArray("Login");
Тhis is the faulty line. Instantiate it correctly.
Something like:
String[] Info = bGuy.getStringArray("Login");