对话框取消使应用程序崩溃

dialog cancel crashes the app

ActivityA中,我有一个图像按钮用于意图ActivityB.

ActivityA

中的部分工作代码
 public void addListenerOnButton() {

        imageButton = (ImageButton) findViewById(R.id.imageButton);

        imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Global.img=null;
                Intent i = new Intent(A.this, B.class);
                startActivityForResult(i, PROJECT_REQUEST_CODE);
            }

        });

    }
     public void onActivityResult(int requestCode,int resultCode, Intent data)
        {
            if(requestCode==PROJECT_REQUEST_CODE) {
             c= data.getStringExtra("text");
                txt1.setText(c);
                viewImage.setImageBitmap(Global.img);

            }
            else if (requestCode==CAMERA_REQUEST_CODE)
            {

            }
        }

ActivityB

  selectImage();
 public void selectImage() {

        final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
        AlertDialog.Builder builder = new AlertDialog.Builder(ImageFitScreen.this);
        builder.setTitle("Add Photo!");
        builder.setItems(options, new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int item) {

                if (options[item].equals("Take Photo"))

                {

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");


                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    //pic = f;

                    startActivityForResult(intent, 1);


                } else if (options[item].equals("Choose from Gallery"))

                {

                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    startActivityForResult(intent, 2);


                } else if (options[item].equals("Cancel")) {

                    dialog.dismiss();
                    finish();

                }

            }

        });

        builder.setOnKeyListener(new Dialog.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK){
                    dialog.dismiss();
                finish();
            }

            return true;
        }
        });
        builder.show();
       }

@Override
public void onBackPressed() {
    Intent returnIntent = new Intent();
    setResult(Activity.RESULT_CANCELED, returnIntent);
    finish();
}

但是当我点击对话框中的 cancel 时,应用程序崩溃了。我确定它崩溃是因为

c= data.getStringExtra("text");

因为 c 删除后它没有崩溃。但是我需要"text" in Activity B return to A.

Ok 按钮在 Activity B

  ok.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View arg0)
            {
                Intent returnIntent=new Intent();
                text=t.getText().toString();
                returnIntent.putExtra("text", text);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

LogCat错误

11-04 13:17:19.321  18437-18437/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 18437
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.example.project.project/com.example.project.project.Project1}: java.lang.NullPointerException
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3681)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3724)
            at android.app.ActivityThread.access00(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.project.project.Project1.onActivityResult(Project1.java:98)

首先你实际上并没有设置"text"的值。但是当你取消 Intent 时仍然是 null 因此你不能做

data.getStringExtra("text")

你应该先检查一下:

if (data !=null && data.hasExtra("text")){
   c= data.getStringExtra("text");
   txt1.setText(c);
}

然后在你的 onbackpressed 中你想像这样设置 "text" extra:

@Override
public void onBackPressed() {
 Intent returnIntent = new Intent();
 returnIntent.putExtra("text", YOUR_STRING_HERE);
 setResult(Activity.RESULT_CANCELED, returnIntent);
 finish();
}

也可能在您的对话框中取消(取决于您要完成的任务)

} else if (options[item].equals("Cancel")) {
   dialog.dismiss();
   Intent returnIntent = new Intent();
   returnIntent.putExtra("text", YOUR_STRING_HERE);
   setResult(Activity.RESULT_CANCELED, returnIntent);
   finish();

}

您没有通过意图向 Activity A 发送任何 text 字段。

设置textonBackPressed()Activity B

@Override
public void onBackPressed() {
    Intent returnIntent = new Intent();
    // Set the text here
    setResult(Activity.RESULT_CANCELED, returnIntent);
    finish();
}

喜欢

@Override
public void onBackPressed() {
    Intent returnIntent = new Intent();
    returnIntent.putExtra("text", YOUR_TEXT);
    setResult(Activity.RESULT_CANCELED, returnIntent);
    finish();
}