返回数据时,“startActivityForResult”启动的活动崩溃

时间:2010-06-16 08:02:44

标签: android

嗨,我用appA用startActivityForResult方法调用了一个应用程序B. 当B尝试将结果返回给A时,应用程序崩溃并且我收到此错误(顺便说一下com.sistematica.carichispeciali.MobiWork是应用程序A,用startActivityForResult调用B的那个)

06-16 09:40:32.506: WARN/dalvikvm(744): threadid=3: thread exiting with uncaught exception (group=0x4001b390)
06-16 09:40:32.506: ERROR/AndroidRuntime(744): Uncaught handler: thread main exiting due to uncaught exception
06-16 09:40:32.526: ERROR/AndroidRuntime(744): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.sistematica.carichispeciali/com.sistematica.carichispeciali.MobiWork}: java.lang.NullPointerException
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3504)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3546)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at android.app.ActivityThread.access$2700(ActivityThread.java:126)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1962)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at android.os.Looper.loop(Looper.java:123)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at android.app.ActivityThread.main(ActivityThread.java:4595)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at java.lang.reflect.Method.invoke(Method.java:521)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at dalvik.system.NativeStart.main(Native Method)
06-16 09:40:32.526: ERROR/AndroidRuntime(744): Caused by: java.lang.NullPointerException
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at com.sistematica.carichispeciali.MobiWork.onActivityResult(MobiWork.java:96)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at android.app.Activity.dispatchActivityResult(Activity.java:3868)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3500)
06-16 09:40:32.526: ERROR/AndroidRuntime(744):     ... 11 more

这是B返回结果的方式

private void risultato(final String paper)
      {
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setIcon(R.drawable.alt);
          builder.setTitle("risultato");
          builder.setMessage("è "+paper+" la targa?");
          builder.setPositiveButton("si", new DialogInterface.OnClickListener(){
              @Override
              public void onClick(DialogInterface dialog, int which)
              {
                dialog.dismiss();
                resultIntent = new Intent();
                resultIntent.putExtra("1", paper);
                setResult(Activity.RESULT_OK, resultIntent);
                finish();
              }
          });
          builder.setNegativeButton("no", new DialogInterface.OnClickListener(){
              @Override
              public void onClick(DialogInterface dialog, int which)
              {
                 delRES();
              }
          });
          builder.show();
      };

这就是A使用它的方式(它应该把返回的字符串放到editText中,从来没有被分析过,看它是否真的有效,因为它之前崩溃了)

  public void onActivityResult(int requestCode, int resultCode, Intent data) {     
      super.onActivityResult(requestCode, resultCode, data); 
      switch(requestCode) { 
        case (1) : { 
          if (resultCode == Activity.RESULT_OK) { 
          String newText = data.getStringExtra("1");
          m_plateField.setText(newText);
          } 
          break; 
        } 
      } 
    }

1 个答案:

答案 0 :(得分:0)

我认为这与这一行有关:

String newText = data.getStringExtra("1");

可能会返回null。

添加额外内容时,请在应用程序包前加上:

resultIntent.putExtra("com.sistematica.carichispeciali.1", paper);

然后将其读作:

String newText = data.getStringExtra("com.sistematica.carichispeciali.1");