

<https://github.com/yabushi/TimeStampCamera>

自分で書いた部分↓  
<https://github.com/yabushi/TimeStampCamera/blob/4b4c441675a6a5e7d6b2cf1f0448e19b1a115fd7/app/src/main/java/com/example/shigeki/timestampcamera/MainActivity.java>

のプログラムをRUNしようとすると「Android端末には問題が発生したのでTimeStampCameraを終了します」と表示され、以下のエラーが出ます。

72行目の
`characteristics.get(CameraCharacteristics.LENS_FACING)`の部分に問題がありそうだとは思いましたが、解決方法がわかりません。  
よろしくお願いします。

[code]

    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.shigeki.timestampcamera, PID: 867
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.shigeki.timestampcamera/com.example.shigeki.timestampcamera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2366)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517)
            at android.app.ActivityThread.access$800(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:189)
            at android.app.ActivityThread.main(ActivityThread.java:5529)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
            at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
            at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
            at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:56)
            at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202)
            at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
            at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
            at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
            at com.example.shigeki.timestampcamera.MainActivity.<init>(MainActivity.java:46)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1572)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517) 
            at android.app.ActivityThread.access$800(ActivityThread.java:162) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
            at android.os.Handler.dispatchMessage(Handler.java:106) 
            at android.os.Looper.loop(Looper.java:189) 
            at android.app.ActivityThread.main(ActivityThread.java:5529) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 
    
[/code]


CC BY-SA 4.0
2018-06-09T10:56:26.290
44647
2018-06-10T22:03:37.620
2018-06-10T12:21:34.690
3060
28851
question
camera nullpointerexception
Unboxing of 'characteristics.get(CameraCharacteristics.LENS_FACING)' may produce 'java.lang.NullPointerException'


元のプログラムはこれです。

[code]

    public class MainActivity extends AppCompatActivity {
    
        ///////////////////////////////////////////////////////////////////////////フィールド////////
    
        //CameraDeviceインスタンス用変数
        CameraDevice mCameraDevice;
    
        //CameraCaptureSession用の変数
        CameraCaptureSession mCaptureSession = null;
    
        //CaptureRequest用変数
        CaptureRequest mPreviewRequest = null;
    
        private CaptureRequest.Builder mPreviewRequestBuilder;
    
            //画面にセットされたTextureView
            final TextureView mTextureView = findViewById(R.id.textureview);
    
            //シャッターボタン
            final Button ShutterButton = findViewById(R.id.shutter);
    
    
        //////////////////////////////////////////////////////////////////////////////////////////
    
        @SuppressLint("MissingPermission")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
[/code]

mTextureViewとShutterButtonの定義位置を下記のように変更すると解決しました。

[code]

    public class MainActivity extends AppCompatActivity {
    
        ///////////////////////////////////////////////////////////////////////////フィールド////////
    
        //CameraDeviceインスタンス用変数
        CameraDevice mCameraDevice;
    
        //CameraCaptureSession用の変数
        CameraCaptureSession mCaptureSession = null;
    
        //CaptureRequest用変数
        CaptureRequest mPreviewRequest = null;
    
        private CaptureRequest.Builder mPreviewRequestBuilder;
    
    
        //////////////////////////////////////////////////////////////////////////////////////////
    
        @SuppressLint("MissingPermission")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //画面にセットされたTextureView
            final TextureView mTextureView = findViewById(R.id.textureview);
    
            //シャッターボタン
            final Button ShutterButton = findViewById(R.id.shutter);
    
[/code]


CC BY-SA 4.0
2018-06-10T09:58:46.847
44661
2018-06-10T22:03:37.620
2018-06-10T22:03:37.620
19110
28851
44647
answer