1. ホーム
  2. アンドロイド

NULLオブジェクト参照で「カメラサービス+ハードウェア.Camera$PreviewCallback)」への接続に失敗する

2022-03-16 04:39:14
<パス

エラー報告モバイル



Nexus 5 Android 6.0 API23



エラーログ

W/CameraBase: An error occurred while connecting to camera: 0
W/System.err: java.lang.RuntimeException: Fail to connect to camera service
W/System.err: at android.hardware.Camera.<init>(Camera.java:495)
W/System.err: at android.hardware.Camera.open(Camera.java:356)
W/System.err: at com.tlhp.transparent.shadow.camera.cam.PreviewCamera.surfaceCreated(PreviewCamera.java:72)
W/System.err: at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
W/System.err: at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
W/System.err: at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
W/System.err: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
W/System.err: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
W/System.err: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
W/System.err: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
W/System.err: at android.view.Choreographer.doCallbacks(Choreographer.java:670)
W/System.err: at android.view.Choreographer.doFrame(Choreographer.java:606)
W/System.err: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at com.safeguarding.freecamera.x.uncaughtException(Unknown Source)
W/System.err: at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)


E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.tlhp.transparent.shadow.camera, PID: 19579
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewCallback(android.hardware.Camera $PreviewCallback)' on a null object reference
        at com.tlhp.transparent.shadow.camera.cam.PreviewCamera.surfaceCreated(PreviewCamera.java:89)
        at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
        at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)


W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.stopPreview()' on a null object reference
W/System.err: at com.tlhp.transparent.shadow.camera.cam.PreviewCamera.surfaceChanged(PreviewCamera.java:89)
W/System.err: at android.view.SurfaceView.updateWindow(SurfaceView.java:593)
W/System.err: at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
W/System.err: at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
W/System.err: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
W/System.err: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
W/System.err: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
W/System.err: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
W/System.err: at android.view.Choreographer.doCallbacks(Choreographer.java:670)
W/System.err: at android.view.Choreographer.doFrame(Choreographer.java:606)
W/System.err: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)



エラー報告の理由



https://developer.android.com/reference/android/hardware/Camera

This class was deprecated in API level 21.
We recommend using the new android.hardware.camera2 API for new applications. 


解決方法



Googleは21日以降にandroid.hardware.camera2 APIを使用するように言っています。



Google公式ドキュメント https://developer.android.com/reference/android/hardware/camera2/package-summary.html



Google Camera2の公式デモ https://github.com/googlesamples/android-Camera2Basic



参考記事 Camera2の使い方 https://www.cnblogs.com/wnpp/articles/7816962.html