onPause()被意外调用

时间:2018-07-25 08:04:19

标签: android android-activity

据我了解,当另一个活动弹出时,会调用update(solutionFile: SolutionFile, file?: File): Observable<SolutionFile> { console.log(solutionFile); let url = `${this.url}/src_uploads/${solutionFile.id}/`; if (file) { let headers = new HttpHeaders({ 'enctype': 'multipart/form-data', 'Authorization': `JWT ${this.authService.getToken()}` }) const formData: FormData = new FormData(); formData.append('student_solution', String(solutionFile.student_solution)); formData.append('file_src', file, file.name); return this.httpClient.put<SolutionFile>(url, formData, { headers: headers }) .pipe( catchError(this.handleError('UPDATE solution-file', new SolutionFile())) ); } return this.httpClient.put<SolutionFile>(url, solutionFile, { headers: this.headers }) .pipe( catchError(this.handleError('UPDATE solution-file', new SolutionFile()) ) ); } 。但就我而言,这种事情没有发生,但是onPause()被调用了,onPause()被调用了。有什么想法为什么会发生?

我在下面发布了整个日志,您可以搜索 D / Camera2VideoFragment:onPause:在onPause 中。

我还发布了我认为相关功能的代码块。

暂停

app crashes

摄像机更改状态时的回调状态。

@Override
    public void onPause() {
        if(MyDebug.LOG)
            Log.d(TAG, "onPause: In onPause");
        closeCamera();
        stopBackgroundThread();
        super.onPause();
    }

开始录像

private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
            @Override
            public void onOpened(@NonNull CameraDevice cameraDevice) {
                if(MyDebug.LOG)
                    Log.d(TAG, "onOpened: This is called: " + System.currentTimeMillis());
                mCameraDevice = cameraDevice;
                //start the preview only if there's been no restart or the video is stopped.
                if(NUM_OF_RESTARTS <= 0)
                    startPreview();
                mCameraOpenCloseLock.release();
                if (null != mTextureView) {
                    configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
                }
                //Starts the video again when max file is reached.
                if(MAX_FILE_REACHED)
                    startRecordingVideo();
            }
            @Override
            public void onDisconnected(@NonNull CameraDevice cameraDevice) {
                if(MyDebug.LOG)
                    Log.d(TAG, "onDisconnected: THIS IS CALLED: " + System.currentTimeMillis());
                mCameraOpenCloseLock.release();
                if(MyDebug.LOG)
                    Log.d(TAG, "onDisconnected: cameraDevice.close() is called here: " + System.currentTimeMillis());
                cameraDevice.close();
                mCameraDevice = null;
            }
            @Override
            public void onError(@NonNull CameraDevice cameraDevice, int error) {
                if(MyDebug.LOG)
                    Log.d(TAG, "onError: This is called with error: " + error+ " at: " + System.currentTimeMillis());
                mCameraOpenCloseLock.release();
                if(MyDebug.LOG)
                    Log.d(TAG, "onError: cameraDevice.close() is called here: " + System.currentTimeMillis());
                cameraDevice.close();
                mCameraDevice = null;
                Activity activity = getActivity();
                if (null != activity) {
                    activity.finish();
                }
            }

LOGCAT

private void startRecordingVideo() {
        final Activity activity = getActivity();
        if (null == activity) {
            return;
        }
        if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
            if(MyDebug.LOG){
                if(mCameraDevice == null)
                    Log.d(TAG, "startRecordingVideo: mCameraDevice is NULL: " + System.currentTimeMillis() + Thread.currentThread().toString());
                if(!mTextureView.isAvailable())
                    Log.d(TAG, "startRecordingVideo: mTextureView is not available");
                if(mPreviewSize == null)
                    Log.d(TAG, "startRecordingVideo: mPreviewSize is NULL");
            }
            return;
        }
        try {
            closePreviewSession();
            // Toast.makeText(activity," before setUpMediaRecorder " , Toast.LENGTH_LONG).show();
            setUpMediaRecorder();
            // Toast.makeText(activity," After setUpMediaRecorder " , Toast.LENGTH_LONG).show();

            /**----------------------------------------------------------------------------------------*/
            /**Setting up setOnInfoListener and setOnErrorListener to
             * handle the video if it reaches max file size or if some error arises.
             */
            mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
                @Override
                public void onInfo(MediaRecorder mr, int what, int extra) {
                    if(MyDebug.LOG)
                        Log.d(TAG, "MediaRecorder onInfo: " + what + " extra: " + extra);
                    final int final_what = what;
                    final int final_extra = extra;
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(MyDebug.LOG)
                                Log.d(TAG, "run: calling onVideoInfo here: " + System.currentTimeMillis());
                            onVideoInfo(final_what, final_extra);
                            if(MyDebug.LOG)
                                Log.d(TAG, "run: returned here after onVideoInfo: " + System.currentTimeMillis());
                        }
                    });
                }
            });

            mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
                @Override
                public void onError(MediaRecorder mr, int what, int extra) {
                    final int final_what = what;
                    final int final_extra  = extra;
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            onVideoError(final_what, final_extra);
                        }
                    });
                }
            });
            /**-----------------------------------------------------------------------------------------*/
            SurfaceTexture texture = mTextureView.getSurfaceTexture();
            assert texture != null;

            // Toast.makeText(activity," setDefaultBufferSize " , Toast.LENGTH_LONG).show();
            texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
            List<Surface> surfaces = new ArrayList<>();

            // Toast.makeText(activity," before Set up Surface " , Toast.LENGTH_LONG).show();
            // Set up Surface for the camera preview
            Surface previewSurface = new Surface(texture);
            surfaces.add(previewSurface);
            mPreviewBuilder.addTarget(previewSurface);

            // Set up Surface for the MediaRecorder
            Surface recorderSurface = mMediaRecorder.getSurface();
            surfaces.add(recorderSurface);
            mPreviewBuilder.addTarget(recorderSurface);

            // Toast.makeText(activity," After mPreviewBuilder" , Toast.LENGTH_LONG).show();
            // Start a capture session
            // Once the session starts, we can update the UI and start recording
            mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {

                @Override
                public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                    mPreviewSession = cameraCaptureSession;
                    // Toast.makeText(activity," before updatePreview" , Toast.LENGTH_LONG).show();
                    updatePreview();
                    // Toast.makeText(activity," After updatePreview" , Toast.LENGTH_LONG).show();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // UI
                            mButtonVideo.setText(R.string.stop);
                            mIsRecordingVideo = true;
                            // Start recording
                            if(mMediaRecorder == null) {
                                if(MyDebug.LOG)
                                    Log.d(TAG, "onConfigured, run: mMediaRecorder is somehow null." );
                            }
                            mMediaRecorder.start();
                        }
                    });
                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                    Activity activity = getActivity();
                    if (null != activity) {
                        Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
                    }
                }
            }, mBackgroundHandler);
        } catch (CameraAccessException | IOException e) {
            e.printStackTrace();
        }

    }

2 个答案:

答案 0 :(得分:1)

在学习Android的第一步时,此图始终帮助我理解生命周期以及调用方法的原因

enter image description here

也许这也可以为您提供帮助,并且您可以找出导致应用程序调用{​​{1}}

的哪些元素

答案 1 :(得分:0)

您mMediaRecorder.stop()的顺序不正确,或者正在导致Camera服务出现问题,从而导致Camera Service的配置不正确。因此,当达到最大文件大小限制并且创建了新曲面并即将进行预览时,由于崩溃对话框位于曲面视图的顶部,并且调用了onPause。