摄像机参数的NullPointerException错误

时间:2013-04-08 11:02:26

标签: android nullpointerexception android-camera

我有以下代码:

Line Code
 193 private Camera mCamera;
 <!-- NO CALLS TO mCamera HERE -->
 229 mCamera = Camera.open();
 230 Camera.Parameters p = mCamera.getParameters();

当我运行应用程序时,我得到以下stacktrace:

04-08 20:56:45.330: E/AndroidRuntime(570): FATAL EXCEPTION: main
04-08 20:56:45.330: E/AndroidRuntime(570): java.lang.NullPointerException
04-08 20:56:45.330: E/AndroidRuntime(570):  at com.soloman.cbc.FullscreenActivity$MySurfaceView.surfaceCreated(FullscreenActivity.java:230)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.SurfaceView.updateWindow(SurfaceView.java:569)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.SurfaceView.access$000(SurfaceView.java:86)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:174)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1842)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.Choreographer.doFrame(Choreographer.java:532)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.os.Handler.handleCallback(Handler.java:725)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.os.Looper.loop(Looper.java:137)
04-08 20:56:45.330: E/AndroidRuntime(570):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-08 20:56:45.330: E/AndroidRuntime(570):  at java.lang.reflect.Method.invokeNative(Native Method)
04-08 20:56:45.330: E/AndroidRuntime(570):  at java.lang.reflect.Method.invoke(Method.java:511)
04-08 20:56:45.330: E/AndroidRuntime(570):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-08 20:56:45.330: E/AndroidRuntime(570):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-08 20:56:45.330: E/AndroidRuntime(570):  at dalvik.system.NativeStart.main(Native Method)

我使用的代码来自How can I manipulate the camera preview?

CAMERA permission中有AndroidManifest.xml,所有imports都已计算在内。有没有人对如何修复此错误有任何建议?

谢谢。

2 个答案:

答案 0 :(得分:3)

在没有后置摄像头Camera.open()的设备上返回null。尝试使用Camera.open(0)获取前置摄像头。

答案 1 :(得分:1)

如果Android版本在3.0之前,请使用

getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);