gpt4 book ai didi

android - onPause() 被意外调用

转载 作者:行者123 更新时间:2023-11-29 23:44:56 37 4
gpt4 key购买 nike

根据我的理解,onPause() 是在另一个 Activity 在它上面弹出时调用的。但在我的例子中,没有发生这样的事情,但是调用了 onPause() 并且 app 崩溃了。想知道为什么会这样吗?

我把整个logcat贴在下面,你可以在里面搜索D/Camera2VideoFragment: onPause: In onPause

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

暂停

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

开始录像

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

}

LOGCAT

 I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2ebe9688 time:2777009
D/ROOT: Root access requested
D/Camera2VideoFragment: tryAcquire
I/CameraManagerGlobal: getCameraService: Reconnecting to camera service
E/Camera2VideoFragment: Couldn't find any suitable preview size
D/Camera2VideoFragment: openCamera: Intialising New MediaRecorder.
D/Camera2VideoFragment: openCamera: Manager.openCamera called here: 1532504895664
I/CameraManager: Using legacy camera HAL.
D/Camera2VideoFragment: onOpened: This is called: 1532504896165
I/CameraDeviceState: Legacy camera service transitioning to state CONFIGURING
I/RequestThread-0: Configure outputs: 1 surfaces configured.
D/Camera: app passed NULL surface
I/CameraDeviceState: Legacy camera service transitioning to state IDLE
I/RequestQueue: Repeating capture request set.
W/LegacyRequestMapper: convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
W/LegacyRequestMapper: convertRequestToMetadata - Ignoring android.lens.focusDistance false, only 0.0f is supported
I/CameraDeviceState: Legacy camera service transitioning to state CAPTURING
E/BufferQueueProducer: [unnamed-8823-1] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=6 undequeued=0)
E/BufferQueueProducer: [unnamed-8823-1] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=5 undequeued=1)
D/Camera2VideoFragment: onClick: NUM_OF_RESTARTS click: 0
I/RequestQueue: Repeating capture request cancelled.
I/CameraDeviceState: Legacy camera service transitioning to state CONFIGURING
I/RequestThread-0: Configure outputs: 2 surfaces configured.
D/Camera: app passed NULL surface
I/CameraDeviceState: Legacy camera service transitioning to state IDLE
I/RequestQueue: Repeating capture request set.
W/LegacyRequestMapper: convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
W/LegacyRequestMapper: convertRequestToMetadata - Ignoring android.lens.focusDistance false, only 0.0f is supported
V/RenderScript: 0xb7cfa0a0 Launching thread(s), CPUs 4
E/BufferQueueProducer: [unnamed-8823-2] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=6 undequeued=0)
I/CameraDeviceState: Legacy camera service transitioning to state CAPTURING
E/BufferQueueProducer: [unnamed-8823-2] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=5 undequeued=1)
D/Camera2VideoFragment: MediaRecorder onInfo: 801 extra: 0
D/Camera2VideoFragment: run: calling onVideoInfo here: 1532504954189
onVideoInfo: what: 801 extra: 0
onVideoInfo: Thread Name: main
onVideoInfo: MAX FILE SIZE APPROACHING
onVideoInfo: Calling stopRecordingVideo: 1532504954189
D/Camera2VideoFragment: restartRecordingVideo: Stoping Video From restart: 1532504954193
stopRecordingVideo: Stop Video Recording
stopRecordingVideo: About to call mMediaRecorder.stop()
W/Adreno-EGLSUB: <DequeueBuffer:736>: dequeue native buffer fail: No such device, buffer=0x0, handle=0x0
W/Adreno-EGL: <qeglDrvAPI_eglSwapBuffers:3709>: EGL_BAD_SURFACE
E/CameraDeviceGLThread-0: Received exception on GL render thread:
java.lang.IllegalStateException: swapBuffers: EGL error: 0x300d
at android.hardware.camera2.legacy.SurfaceTextureRenderer.checkEglError(SurfaceTextureRenderer.java:530)
at android.hardware.camera2.legacy.SurfaceTextureRenderer.swapBuffers(SurfaceTextureRenderer.java:523)
at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:729)
at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:105)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
I/CameraDeviceState: Legacy camera service transitioning to state ERROR
D/Camera2VideoFragment: stopRecordingVideo: Done mMediaRecorder.stop()
stopRecordingVideo: Reset mMediaRecorder
stopRecordingVideo: Release mMediaRecorder
D/Camera2VideoFragment: stopRecordingVideo: Closing Camera: 1532504954421
closeCamera: mCameraDevice.close() called here.
E/RequestThread-0: Timed out while waiting for request to complete.
W/CaptureCollector: Preview buffers dropped for request: 1
E/CameraDeviceState: Cannot receive result while in state: 0
Cannot receive result while in state: 0
E/CameraDeviceState: Cannot receive result while in state: 0
W/MessageQueue: Handler (android.graphics.SurfaceTexture$1) {63c0706} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.graphics.SurfaceTexture$1) {63c0706} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:325)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.sendEmptyMessageDelayed(Handler.java:534)
at android.os.Handler.sendEmptyMessage(Handler.java:519)
at android.graphics.SurfaceTexture.postEventFromNative(SurfaceTexture.java:368)
E/BufferQueueProducer: [unnamed-8823-2] cancelBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-2] cancelBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-2] cancelBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-2] cancelBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-2] cancelBuffer: BufferQueue has been abandoned
D/Camera2VideoFragment: stopRecordingVideo: Opening Camera: 1532504958414
D/Camera2VideoFragment: tryAcquire
E/Camera2VideoFragment: Couldn't find any suitable preview size
D/Camera2VideoFragment: openCamera: Intialising New MediaRecorder.
openCamera: Manager.openCamera called here: 1532504958422
I/CameraManager: Using legacy camera HAL.
D/Camera2VideoFragment: onVideoInfo: came back here after restarting video recording: 1532504958920
run: returned here after onVideoInfo: 1532504958920
D/Camera2VideoFragment: onError: This is called with error: 1 at: 1532504958924
onError: cameraDevice.close() is called here: 1532504958924
D/Camera2VideoFragment: onClosed: Camera Closed: 1532504958954
D/Camera2VideoFragment: onOpened: This is called: 1532504958991
I/CameraDeviceState: Legacy camera service transitioning to state CONFIGURING
I/RequestThread-0: Configure outputs: 2 surfaces configured.
D/Camera: app passed NULL surface
I/CameraDeviceState: Legacy camera service transitioning to state IDLE
D/Camera2VideoFragment: onPause: In onPause
I/RequestQueue: Repeating capture request set.
W/LegacyRequestMapper: convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
convertRequestToMetadata - Ignoring android.lens.focusDistance false, only 0.0f is supported
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
E/BufferQueueProducer: [unnamed-8823-3] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=6 undequeued=0)
I/CameraDeviceState: Legacy camera service transitioning to state CAPTURING
E/BufferQueueProducer: [unnamed-8823-0] dequeueBuffer: BufferQueue has been abandoned
W/Adreno-EGLSUB: <DequeueBuffer:736>: dequeue native buffer fail: No such device, buffer=0x0, handle=0x0
W/Adreno-EGL: <qeglDrvAPI_eglSwapBuffers:3709>: EGL_BAD_SURFACE
E/CameraDeviceGLThread-0: Received exception on GL render thread:
java.lang.IllegalStateException: swapBuffers: EGL error: 0x300d
at android.hardware.camera2.legacy.SurfaceTextureRenderer.checkEglError(SurfaceTextureRenderer.java:530)
at android.hardware.camera2.legacy.SurfaceTextureRenderer.swapBuffers(SurfaceTextureRenderer.java:523)
at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:729)
at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:105)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
I/CameraDeviceState: Legacy camera service transitioning to state ERROR
D/Camera2VideoFragment: onError: This is called with error: 1 at: 1532504959666
D/Camera2VideoFragment: onError: cameraDevice.close() is called here: 1532504959667
E/BufferQueueProducer: [unnamed-8823-3] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=5 undequeued=1)
E/RequestThread-0: Timed out while waiting for request to complete.
W/CaptureCollector: Preview buffers dropped for request: 0
E/CameraDeviceState: Cannot receive result while in state: 0
Cannot receive result while in state: 0
E/CameraDeviceState: Cannot receive result while in state: 0
E/BufferQueueProducer: [unnamed-8823-3] queueBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-3] dequeueBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-3] cancelBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-3] cancelBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-3] cancelBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-3] cancelBuffer: BufferQueue has been abandoned
E/BufferQueueProducer: [unnamed-8823-3] cancelBuffer: BufferQueue has been abandoned
D/Camera2VideoFragment: onClosed: Camera Closed: 1532504963772
W/CameraCaptureSession: Session 1: The camera device was already closed:
java.lang.IllegalStateException: CameraDevice was already closed
at android.hardware.camera2.impl.CameraDeviceImpl.checkIfCameraClosedOrInError(CameraDeviceImpl.java:1482)
at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:677)
at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:328)
at android.hardware.camera2.impl.CameraCaptureSessionImpl.finalize(CameraCaptureSessionImpl.java:561)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:191)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:174)
at java.lang.Thread.run(Thread.java:818)

最佳答案

在我学习 Android 的第一步时,这张图总是帮助我理解生命周期和调用方法的原因

enter image description here

也许这对您也有帮助,您可以找出哪些元素导致您的应用调用 OnPause()

关于android - onPause() 被意外调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51513814/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com