我在关于Camera对象为空的crashlytics上收到大量崩溃报告。在测试和使用应用程序时,它不会发生在我身上。
Fatal Exception: java.lang.RuntimeException: Unable to resume activity {packageName/activityName}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3026)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3057)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
at activityName.o(SourceFile:612)
at activityName.u(SourceFile:772)
at activityName.onResume(SourceFile:539)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
at android.app.Activity.performResume(Activity.java:6106)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3015)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3057)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
崩溃发生在camera.getParameters()
。
所以它基本上是因为相机由于某种原因暂时无法连接?
是否与您在手机上的相机应用中遇到的"Can't connect to camera."错误类似?
希望有人可以对此有所了解并分享一些方法来解决这个问题。
答案 0 :(得分:1)
1)您是否正在初始化相机?
camera = camera.open();
2)您是否使用过相机权限?
<uses-permission android:name="android.permission.CAMERA"/>
3)确保在立即打开相机后不要这样做:
camera = null;
4)尝试在manifest.xml中明确地以纵向模式设置相机的方向
5)如果您使用的是Marshmellow,则需要运行时权限才能访问相机。
答案 1 :(得分:0)
我有同样的问题。 如果您将相机用于活动或碎片状态,则应释放它。 就我而言,我没有在onDestroy函数上添加“ mCamera.stopPreview();和mCamera.release()”。
https://developer.android.com/training/camera/cameradirect#java
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
if (mCamera != null) {
// Call stopPreview() to stop updating the preview surface.
mCamera.stopPreview();
}
}
/**
* When this function returns, mCamera will be null.
*/
private void stopPreviewAndFreeCamera() {
if (mCamera != null) {
// Call stopPreview() to stop updating the preview surface.
mCamera.stopPreview();
// Important: Call release() to release the camera for use by other
// applications. Applications should release the camera immediately
// during onPause() and re-open() it during onResume()).
mCamera.release();
mCamera = null;
}
}