对话框取消崩溃应用程序

时间:2015-11-04 05:13:07

标签: java android android-intent start-activity

活动A 中,我有一个用于展示活动B 的图片按钮。

活动A

中部分工作代码
 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)
            {

            }
        }

活动B

  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删除后没有崩溃。但我需要活动B中的“文本”返回A。

活动B

中的

Ok按钮

  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.access$1400(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)

2 个答案:

答案 0 :(得分:1)

首先,您实际上并没有设置" text"的值。但是当你取消时,Intent为null,因此你无法做到

data.getStringExtra("text")

你应该先检查一下:

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

然后在你的onbackpressed你想要设置"文本"额外如此:

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

}

答案 1 :(得分:1)

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

text

中的onBackPressed()中设置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();
}