我正在尝试在手机背景上运行摄像机。但是出现以下错误:
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)
相机服务类代码:
清单:
<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" />
请帮助!
答案 0 :(得分:0)
您的问题是 mCamera
对象为空。简单的解决方法是:
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.");
}
答案 1 :(得分:0)
只需尝试一下...
Camera mcamera = Camera.open(0);