Android相机空指针

时间:2012-04-10 14:59:47

标签: android camera nullpointerexception

我正在尝试设置我的相机视图,但我的应用程序给了我一个错误。我认为这里的问题在于相机ID,我不清楚如何获取相机ID。

04-10 16:59:03.374: E/AndroidRuntime(19562): FATAL EXCEPTION: main
04-10 16:59:03.374: E/AndroidRuntime(19562): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.petkovski.mk/com.petkovski.mk.TransparentSmsActivity}: java.lang.NullPointerException
04-10 16:59:03.374: E/AndroidRuntime(19562):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at android.os.Looper.loop(Looper.java:130)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at android.app.ActivityThread.main(ActivityThread.java:3835)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at java.lang.reflect.Method.invokeNative(Native Method)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at java.lang.reflect.Method.invoke(Method.java:507)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at dalvik.system.NativeStart.main(Native Method)
04-10 16:59:03.374: E/AndroidRuntime(19562): Caused by: java.lang.NullPointerException
04-10 16:59:03.374: E/AndroidRuntime(19562):    at com.petkovski.mk.TransparentSmsActivity.setCameraDisplayOrientation(TransparentSmsActivity.java:78)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at com.petkovski.mk.TransparentSmsActivity.onCreate(TransparentSmsActivity.java:52)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-10 16:59:03.374: E/AndroidRuntime(19562):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
04-10 16:59:03.374: E/AndroidRuntime(19562):    ... 11 more

错误告诉我错误在于此方法

public static void setCameraDisplayOrientation(Activity activity,
             int cameraId, android.hardware.Camera camera) {
         android.hardware.Camera.CameraInfo info =
                 new android.hardware.Camera.CameraInfo();
         android.hardware.Camera.getCameraInfo(cameraId, info);
         int rotation = activity.getWindowManager().getDefaultDisplay()
                 .getRotation();
         int degrees = 0;
         switch (rotation) {
             case Surface.ROTATION_0: degrees = 0; break;
             case Surface.ROTATION_90: degrees = 90; break;
             case Surface.ROTATION_180: degrees = 180; break;
             case Surface.ROTATION_270: degrees = 270; break;
         }

         int result;
         if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
             result = (info.orientation + degrees) % 360;
             result = (360 - result) % 360;  // compensate the mirror
         } else {  // back-facing
             result = (info.orientation - degrees + 360) % 360;
         }
         camera.setDisplayOrientation(result);  //here is the error
     }

我这样称呼这个方法:

setCameraDisplayOrientation(this,cameraInfo.CAMERA_FACING_BACK, camera);

有人可以告诉我这是什么问题吗?

0 个答案:

没有答案