在片段中设置 ProgressBar 可见性时出错 只有创建视图层次结构的原始线程才能接触其视图

Error setting ProgressBar visibility in fragment Only the original thread that created a view hierarchy can touch its views

在我的 camera2 片段中我有这个方法,在 "bar.setvisibility(visibile)" 上我得到:android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及它的视图。

我该如何解决?

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

}

您应该从 UI 线程更新您的 UI。 试试这个,

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