【问题标题】:AlertDialog: Attempted to finish an input event but the input event receiver has already been disposedAlertDialog:尝试完成输入事件,但输入事件接收器已被释放
【发布时间】:2016-08-21 06:17:57
【问题描述】:

当我按下“选择照片”按钮时,AlertDialog 似乎取消了,我在 logcat 中收到以下消息:Attempted to finish an input event but the input event receiver has already been disposed

这在昨晚确实有效,我不记得对这个功能进行了任何更改。请帮忙。

private void dispatchTakePictureIntent() {
    final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};
    final AlertDialog.Builder cameraChoice = new AlertDialog.Builder(Person2Screen.this);
    cameraChoice.setTitle("Take/choose new photo");
    cameraChoice.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (options[which].equals("Take Photo")) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                //savePicture(cameraIntent);
                if(id.equals(HAPPY_ID2))
                    startActivityForResult(cameraIntent, REQUEST_HAPPY_PHOTO);
                if(id.equals(SURPRISED_ID2))
                    startActivityForResult(cameraIntent, REQUEST_SURPRISED_PHOTO);
                if(id.equals(AFRAID_ID2))
                    startActivityForResult(cameraIntent, REQUEST_AFRAID_PHOTO);
                if(id.equals(UPSET_ID2))
                    startActivityForResult(cameraIntent, REQUEST_UPSET_PHOTO);
                if(id.equals(SAD_ID2))
                    startActivityForResult(cameraIntent, REQUEST_SAD_PHOTO);
            } else if (options[which].equals("Choose Photo")) {
                    Intent galleryIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    if(id.equals(HAPPY_ID2))
                        startActivityForResult(galleryIntent, REQUEST_HAPPY_PHOTO);
                    else if(id.equals(SURPRISED_ID2))
                        startActivityForResult(galleryIntent, REQUEST_SURPRISED_PHOTO);
                    else if(id.equals(AFRAID_ID2))
                        startActivityForResult(galleryIntent, REQUEST_AFRAID_PHOTO);
                    else if(id.equals(UPSET_ID2))
                        startActivityForResult(galleryIntent, REQUEST_UPSET_PHOTO);
                    else if(id.equals(SAD_ID2))
                        startActivityForResult(galleryIntent, REQUEST_SAD_PHOTO);
            } else if (options[which].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    final CharSequence[] pictureNumbers = {"Picture 1", "Picture 2", "Picture 3", "Reinforcer"};
    AlertDialog.Builder selectPhotoNumber = new AlertDialog.Builder(Person2Screen.this);
    selectPhotoNumber.setTitle("Which picture would you like to set/change?");
    selectPhotoNumber.setItems(pictureNumbers, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (pictureNumbers[which].equals("Picture 1")) {
                index = 1;
                cameraChoice.show();
            } else if (pictureNumbers[which].equals("Picture 2")) {
                index = 2;
                cameraChoice.show();
            } else if (pictureNumbers[which].equals("Picture 3")) {
                index = 3;
                cameraChoice.show();
            } else if (pictureNumbers[which].equals("Reinforcer")) {
                index = 4;
                cameraChoice.show();
            }
        }
    });
    selectPhotoNumber.show();
}

【问题讨论】:

  • 尝试在startActivityForResult()之后立即调用return
  • @Shaishav 没用 :(
  • 我的并没有真正崩溃。如果您使用的是 Instant Run,请手动删除该应用并重试。顺便说一句,目前你的requestCode 都是一样的。
  • @Shaishav 手动卸载无效。我添加了代码来为 AlertDialog 提供更多上下文。我打电话给另一个。所有其他按钮功能都可以正常工作;只是不是“选择图片”那个...

标签: java android android-alertdialog


【解决方案1】:

我应该早点注意到这种方式,但我猜你正确地发布了你之前的代码。当前的问题是我们正在尝试将"Choose from Gallery""Choose Photo" 匹配,而"Choose Photo" 始终是false。所以,我们需要纠正它。我还稍微重构了您的代码:

private void dispatchTakePictureIntent() { 
    final CharSequence[] options = {"Take Photo", "Choose Photo", "Cancel"};
    final AlertDialog.Builder cameraChoice = new AlertDialog.Builder(Person2Screen.this);
    cameraChoice.setTitle("Take/choose new photo");
    cameraChoice.setItems(options, new DialogInterface.OnClickListener() {
        @Override 
        public void onClick(DialogInterface dialog, int which) {
            if (options[which].equals("Cancel")) {
                dialog.dismiss();
            } else {
                Intent intent;
                int requestCode;

                if (options[which].equals("Take Photo")) {
                     intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                } else { // from doc
                     intent = new Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                }

                switch (id) {
                     case HAPPY_ID2:
                         requestCode = REQUEST_HAPPY_PHOTO;
                         break;
                     case SURPRISED_ID2:
                         requestCode = REQUEST_SURPRISED_PHOTO;
                         break;
                     case AFRAID_ID2:
                         requestCode = REQUEST_AFRAID_PHOTO;
                         break;
                     case UPSET_ID2:
                         requestCode = REQUEST_UPSET_PHOTO;
                         break;
                     default:
                         requestCode = REQUEST_SAD_PHOTO;
                         break;
                }

                startActivityForResult(intent, requestCode);
            }
        } 
    }); 

【讨论】:

  • 难以置信。感谢您了解这一点并进行重构;将使用这个!
  • @ImagineThat Cool...关于您的编辑,我们已经有了 REQUEST_HAPPY_PHOTO...所以,最后一个不应该是 SAD 吗?
猜你喜欢
  • 2016-03-02
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 2017-08-26
  • 1970-01-01
相关资源
最近更新 更多