0

私のカメラ2のフラグメント内にこのmetodがあり、 "bar.setvisibility(visibile)"に表示されます:android.view.ViewRootImpl $ CalledFromWrongThreadException:ビュー階層を作成した元のスレッドのみがそのビューに触れることができます。フラグメント内のProgressBarの可視性の設定ビューの階層を作成した元のスレッドのみがそのビューにアクセスできます

どうすれば解決できますか?

private void captureStillPicture() { 
    try { 
     final Activity activity = getActivity(); 
     if (null == activity || null == mCameraDevice) { 
      return; 
     } 
     // This is the CaptureRequest.Builder that we use to take a picture. 
     final CaptureRequest.Builder captureBuilder = 
       mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); 
     captureBuilder.addTarget(mImageReader.getSurface()); 

     // Use the same AE and AF modes as the preview. 
     captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, 
       CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); 
     setAutoFlash(captureBuilder); 

     // Orientation 
     int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 
     captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation)); 

     CameraCaptureSession.CaptureCallback CaptureCallback 
       = new CameraCaptureSession.CaptureCallback() { 

      @Override 
      public void onCaptureCompleted(@NonNull CameraCaptureSession session, 
              @NonNull CaptureRequest request, 
              @NonNull TotalCaptureResult result) { 
       //showToast("Saved: " + mFile); 
       Log.d(TAG, mFile.toString()); 
      } 
     }; 

     AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity()); 
     builder1.setMessage("Assicurati che la congiuntiva è completamente contenuta nel riquadro verde. Hai scattato correttamente la foto?"); 
     builder1.setCancelable(true); 
     builder1.setPositiveButton(
       "Si", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         bar.setVisibility(View.VISIBLE); 

         //dialog = ProgressDialog.show(PreviewDemo.this, "", "Taglio dell'immagine in corso"); 
         /*ProgressDialog dialog2 = new ProgressDialog(getActivity()); 
         dialog2.setMessage("ciao"); 
         dialog2.setCancelable(false); 
         dialog2.show();*/ 
         File imageFileNameRotated=null; 
         imageFileNameRotated = new File(imageFileFolder, date.toString() + "_ROTATED" + ".jpg"); 
         Bitmap bmp = BitmapFactory.decodeFile(mFile.getPath()); 
         Bitmap bmrt= bmp.copy(bmp.getConfig(),true); 
         CutPhoto.cut(bmrt,imageFileNameRotated.toString()); 
         Intent end=new Intent(); 
         end.putExtra("image",imageFileNameRotated.toString()); 
         activity.setResult(REQUEST_TAKE_PHOTO,end); 
         activity.finish(); 
        } 
       }); 

     builder1.setNegativeButton(
       "No", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         boolean delete=mFile.delete(); 
         activity.finish(); 
         Intent intent=new Intent(getActivity(),CameraActivity.class); 
         startActivity(intent); 

        } 
       }); 
     AlertDialog alert=builder1.create(); 
     alert.getWindow().setGravity(Gravity.BOTTOM); 
     alert.show(); 



     mCaptureSession.stopRepeating(); 
     mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null); 


    } catch (CameraAccessException e) { 
     e.printStackTrace(); 
    } 

} 

答えて

0

UIスレッドからUIを更新する必要があります。 これを試してください、

getActivity().runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      bar.setVisibility(View.VISIBLE); 
     } 
    }); 
+0

本当にthx男! – Jt1995

関連する問題