java.lang.NullPointerException:尝试调用虚方法' android.hardware.Camera $参数android.hardware.Camera.getParameters()

时间:2016-04-07 11:58:12

标签: java android camera

我想在Android M中使用旧的相机api,但是当我运行我的应用程序时,我得到这个空指针异常:

java.lang.RuntimeException: Unable to resume activity {com.in.ri.instacam/com.in.ri.in.Camera.ActivityCamera}: 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:3328)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3359)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2549)
    at android.app.ActivityThread.access$900(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:168)
    at android.app.ActivityThread.main(ActivityThread.java:5845)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
    at com.instacam.riatech.instacam.Camera.ActivityCamera$CameraLoader.setUpCamera(ActivityCamera.java:715)
    at com.instacam.riatech.instacam.Camera.ActivityCamera$CameraLoader.onResume(ActivityCamera.java:700)
    at com.instacam.riatech.instacam.Camera.ActivityCamera.onResume(ActivityCamera.java:320)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1326)
    at android.app.Activity.performResume(Activity.java:6323)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3311)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3359) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2549) 
    at android.app.ActivityThread.access$900(ActivityThread.java:150) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:168) 
    at android.app.ActivityThread.main(ActivityThread.java:5845) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

我的代码是

 private void setUpCamera(final int id) {
        mCameraInstance = getCameraInstance(id);
        Parameters parameters = mCameraInstance.getParameters();
        // TODO adjust by getting supportedPreviewSizes and then choosing
        // the best one for screen size (best fill screen)
        if (parameters.getSupportedFocusModes().contains(
                Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
            parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        }
        mCameraInstance.setParameters(parameters);

        int orientation = mCameraHelper.getCameraDisplayOrientation(
                ActivityCamera.this, mCurrentCameraId);
        CameraHelper.CameraInfo2 cameraInfo = new CameraHelper.CameraInfo2();
        mCameraHelper.getCameraInfo(mCurrentCameraId, cameraInfo);
        boolean flipHorizontal = cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT;
        mGPUImage.setUpCamera(mCameraInstance, orientation, flipHorizontal, false);
    }

我可以在Android M中使用较旧的相机api吗?相同的代码在Android的M(API 6)版本下工作正常。

0 个答案:

没有答案