使用 StartActivityForResult 和 onResultReceive 我收到错误。有什么问题吗?

发布于 2024-11-28 11:25:38 字数 3731 浏览 1 评论 0原文

给出结果的活动:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她说她爱他 2024-12-05 11:25:38

您的代码中的这一行有问题:

 Info = bGuy.getStringArray("Login");

var 没有名称。

但您的问题可能来自于您在整理活动中使用捆绑包的方式。
尝试

if( login.getText() == null || Senha.getText() == null )
{
  //Toast and return
}//if

Intent i = new Intent();
Bundle gGuy = i.getExtras();

gGuy.putStringArray("Login", new String[] {Login.getText().toString(), Senha.getText().toString()});
//gGuy.putString("Login", Login.getText().toString());
//gGuy.putString("Senha", Senha.getText().toString());
setResult(RESULT_OK, i);
finish();

然后你可以按照你已经做的方式在 OnActivityResult 中恢复你的数组(请添加 @Override 注释)。

请阅读有关 java 中的命名约定的内容。

问候,
斯特凡

You have a problem at this line in your code :

 Info = bGuy.getStringArray("Login");

var has no name.

But your problem might come from the way you use your bundle in the finishing activity.
Try

if( login.getText() == null || Senha.getText() == null )
{
  //Toast and return
}//if

Intent i = new Intent();
Bundle gGuy = i.getExtras();

gGuy.putStringArray("Login", new String[] {Login.getText().toString(), Senha.getText().toString()});
//gGuy.putString("Login", Login.getText().toString());
//gGuy.putString("Senha", Senha.getText().toString());
setResult(RESULT_OK, i);
finish();

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

放低过去 2024-12-05 11:25:38

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");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文