尝试在空对象引用上调用虚拟方法“void android.hardware.Camera.stopPreview()”

Posted

技术标签:

【中文标题】尝试在空对象引用上调用虚拟方法“void android.hardware.Camera.stopPreview()”【英文标题】:Attempt to invoke virtual method 'void android.hardware.Camera.stopPreview()' on a null object reference 【发布时间】:2019-03-29 17:41:33 【问题描述】:

我正在尝试在手机背景上运行摄像机。但是得到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.stopPreview()' on a null object reference
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3230)
    at android.app.ActivityThread.-wrap17(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5737)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.stopPreview()' on a null object reference
    at bd.org.rain.lostphonefinder.CameraService.handleStopRecordingCommand(CameraService.java:241)
    at bd.org.rain.lostphonefinder.CameraService.onStartCommand(CameraService.java:104)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3213)
    at android.app.ActivityThread.-wrap17(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:5737) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

相机服务类代码:

https://pastebin.com/WKKPafD3

清单:

<uses-feature android:name="android.hardware.camera2.full" />
<uses-feature android:name="android.hardware.camera2.front" android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

请帮忙!

【问题讨论】:

在某些时候你的相机会变成null What is a NullPointerException, and how do I fix it?的可能重复 【参考方案1】:

您的问题是 mCamera 对象为 null。简单的解决方法是:

private void handleStopRecordingCommand(Intent intent) 
    ResultReceiver resultReceiver = intent.getParcelableExtra(RESULT_RECEIVER);

    if (!mRecording) 
        // have not recorded
        resultReceiver.send(RECORD_RESULT_NOT_RECORDING, null);
        return;
    

    try 
        mMediaRecorder.stop();
        mMediaRecorder.release();
     catch (RuntimeException e) 
        mMediaRecorder.reset();
        resultReceiver.send(RECORD_RESULT_UNSTOPPABLE, new Bundle());
        return;
     finally 
        mMediaRecorder = null;
        if(mCamera != null) // This will prevent null pointer exception
            mCamera.stopPreview();
            mCamera.release();
        
        mRecording = false;
    

    Bundle b = new Bundle();
    b.putString(VIDEO_PATH, mRecordingPath);
    resultReceiver.send(RECORD_RESULT_OK, b);

    Log.d(TAG, "recording is finished.");

【讨论】:

【参考方案2】:

试试这个……

Camera mcamera = Camera.open(0);

【讨论】:

【参考方案3】:

您需要获得相机权限 实现 detrex 库

    implementation 'com.karumi:dexter:4.2.0'

然后在下面获取权限

   Dexter.withActivity(this)
            .withPermissions(
                    android.Manifest.permission.CAMERA,

                    android.Manifest.permission.VIBRATE,
                    android.Manifest.permission.READ_PHONE_STATE)
            .withListener(new MultiplePermissionsListener() 
                @Override
                public void onPermissionsChecked(MultiplePermissionsReport report) 
                    // check if all permissions are granted
                    if (report.areAllPermissionsGranted()) 
                        // do you work now
                    

                    // check for permanent denial of any permission
                    if (report.isAnyPermissionPermanentlyDenied()) 
                        // permission is denied permenantly, navigate user to app settings
                    
                

                @Override
                public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) 
                    token.continuePermissionRequest();
                
            )
            .onSameThread()
            .check();

也将权限粘贴到清单中

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<permission
    android:name="android.permission.FLASHLIGHT"
    android:permissionGroup="android.permission-group.HARDWARE_CONTROLS" />
enter code here

【讨论】:

以上是关于尝试在空对象引用上调用虚拟方法“void android.hardware.Camera.stopPreview()”的主要内容,如果未能解决你的问题,请参考以下文章

尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'

NullPointerException 错误尝试在空对象引用上调用虚拟方法错误

Android错误尝试在空对象引用上调用虚拟方法[重复]

AsyncTask +数据库尝试在空对象引用上调用虚拟方法[重复]

数据库错误:尝试在空对象引用上调用虚拟方法“Cursor .getScene()”[重复]

尝试在空对象引用上调用虚拟方法 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()'