gpt4 book ai didi

android - 返回 Activity 后 CameraSource 触发空异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:26 25 4
gpt4 key购买 nike

我有一个初始化条形码检测的 Activity 。然后我转到另一个 Activity ,当我按下后退按钮时,它会触发此异常的循环:

com.example.myapp E/CameraSource: Exception thrown from receiver.
java.lang.NullPointerException
at com.google.android.gms.vision.CameraSource$zzb.run(Unknown Source)
at java.lang.Thread.run(Thread.java:841)

如果我坚持并再次进入另一个 Activity ,它会触发另一个异常:

com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapp, PID: 22942
java.lang.NullPointerException
at com.google.android.gms.vision.CameraSource$zzb.release(Unknown Source)
at com.google.android.gms.vision.CameraSource.release(Unknown Source)
at com.example.myapp.CameraActivity$3.surfaceDestroyed(CameraActivity.java:184)
at android.view.SurfaceView.updateWindow(SurfaceView.java:586)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:240)
at android.view.View.dispatchWindowVisibilityChanged(View.java:8495)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6602)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:805)
at android.view.Choreographer.doCallbacks(Choreographer.java:605)
at android.view.Choreographer.doFrame(Choreographer.java:575)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:791)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)

CameraSource 在 onCreate 中是这样初始化的:

    final CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
.setRequestedFps(15f)
.setAutoFocusEnabled(true)
.build();
mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
return;
cameraSource.start(holder);
} catch (IOException e) {
Log.e(getClass().getName(), e.getMessage());
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.release();
}
});

最佳答案

我将相机初始化代码从 onCreate 移到了 onResume 并像这样保护相机释放:

mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
...
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCameraSource != null) {
mCameraSource.release();
mCameraSource = null;
}
}
});

现在可以了。

关于android - 返回 Activity 后 CameraSource 触发空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38982826/

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