2016-08-20 48 views
1

當我按下「選擇圖片」按鈕,AlertDialog似乎取消了,我得到了logcat的以下信息:Attempted to finish an input event but the input event receiver has already been disposedAlertDialog:試圖完成一個輸入事件,但輸入的事件接收器已被釋放

這是從昨晚開始工作,我不記得有關此功能的任何更改。請幫忙。

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(); 
} 
+0

嘗試在'startActivityForResult()'後面調用'return'。 – Shaishav

+0

@ Shaishav沒有工作:( – rafvasq

+0

它並沒有真正崩潰在我的。如果你使用即時運行,手動刪除應用程序,然後重試。順便說一句,目前這兩個'requestCode'是相同的。 – Shaishav

回答

1

我應該早點注意到這種方式,但是,我想你已經正確地發佈了你以前的代碼。目前的問題是,我們試圖匹配"Choose from Gallery""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); 
      } 
     } 
    }); 
+0

令人難以置信。爲了捕捉那個和重構;將會使用這個! – rafvasq

+1

@ImagineThat酷...關於你的編輯,我們已經有了'REQUEST_HAPPY_PHOTO' ...所以,最後一個不應該是SAD? – Shaishav

相關問題