使用StartActivityForResult和onResultReceive我收到一个错误。有什么不对?

时间:2011-08-09 17:45:34

标签: android android-activity

给出结果的活动:

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

2 个答案:

答案 0 :(得分:1)

您的代码中此行存在问题:

 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注释)。

请阅读naming conventions in java

此致  斯蒂芬

答案 1 :(得分:0)

Info = bGuy.getStringArray("Login");

这是错误的路线。正确实例化。

类似的东西:

String[] Info = bGuy.getStringArray("Login");