Android相机在旋转时失败

时间:2015-02-10 13:06:16

标签: android camera

我在LG Nexus 5(Android 5.0.1(android-21))上有这个错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.lock()' on a null object reference
    at video.LayRecordVideo.releaseMediaRecorder(LayRecordVideo.java:511)
    at video.LayRecordVideo.stopRecord(LayRecordVideo.java:569)
    at video.LayRecordVideo.access$3(LayRecordVideo.java:554)
    at video.LayRecordVideo$1.handleMessage(LayRecordVideo.java:249)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    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:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

private void releaseMediaRecorder()
    {
        if (mMediaRecorder != null)
        {
            mMediaRecorder.reset(); // clear recorder configuration
            mMediaRecorder.release(); // release the recorder object
            mMediaRecorder = null;
            mCamera.lock(); // this is line 511
        }
    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

最简单的解决方法是将android:configChanges="orientation|screenSize"添加到AndroidManifest.xml文件中的活动中。例如:

<activity 
 android:name=".YourActivity"
 android:configChanges="orientation|screenSize" />

它将阻止在配置更改(例如旋转屏幕)上重新创建活动。另一种解决方案是在您的活动中自行处理onConfigurationChanged(Configuration newConfig)方法中的配置更改。

我不确定这个解决方案是否会有所帮助。