4

我一直在尝试让我的应用程序上的相机部分工作大约一周,在最终修复了一个错误之后,它创建了一个新的。

当我恢复活动时,预览不起作用,录制功能也不起作用。通过查看 Logcat,我发现我的Camera对象等于 null,我找不到重新创建它的方法。

我的创作

mCamera = getCameraInstance();    
// Create preview view and set it as the content of our activity.    
mPreview = new CameraPreview(this, mCamera);    
preview = (FrameLayout) findViewById(R.id.previewer);   
preview.addView(mPreview);

获取相机实例方法

private Camera getCameraInstance() { 

        Camera c = null;     
        try {     
            //c = Camera.open(); // attempt to get a Camera instance    
             c = this.open(); // attempt to get a Camera instance    
        } catch (Exception e) {     
            // Camera is not available (in use or does not exist)    
        }     
        Log.i("TEST getCameraInstance", "" + c);     
        return c; // returns null if camera is unavailable          
}

我的暂停

protected void onPause() {

        super.onPause();      
        try {     
            // release the camera immediately on pause event     
            // releaseCamera();    
            mCamera.stopPreview();      
            mCamera.setPreviewCallback(null);    
            mCamera.release();     
            mCamera = null;     
        } catch (Exception e) {       
            e.printStackTrace();    
        }    
}

我的简历

protected void onResume() {

        super.onResume();       
        try {       
            mCamera = Camera.open();      
            mCamera = getCameraInstance();     
            Log.i("TEST onresume", "onResume Runing");      
            Log.i("TEST onresume", "camera "  + mCamera);       
            mCamera.setPreviewCallback(null);      
            Log.i("TEST onresume", "camera "  + mCamera);      
            //mCamera = getCameraInstance();      
            Log.i("TEST onresume", "camera "  + mCamera);    
            // mCamera.setPreviewCallback(null);     
            mPreview = new CameraPreview(MainRecordScreen.this, mCamera);// set preview      
            Log.i("TEST onresume", "camera "  + mCamera);                                                                               
            preview.addView(mPreview);     
            Log.i("TEST onresume", "camera "  + mCamera);    
            mCamera.startPreview();              
        } catch (Exception e) {      
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());    
        }      
        Log.i("TEST onresume", "camera "  + mCamera);    
}

我的日志猫

06-19 06:38:40.223: E/AndroidRuntime(16702): FATAL EXCEPTION: main
06-19 06:38:40.223: E/AndroidRuntime(16702): java.lang.NullPointerException
06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.plcd.test.MainRecordScreen.prepareVideoRecorder(MainRecordScreen.java:198)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.plcd.test.MainRecordScreen.access$7(MainRecordScreen.java:193)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.plcd.test.MainRecordScreen$3.onClick(MainRecordScreen.java:115)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.view.View.performClick(View.java:3511)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.view.View$PerformClick.run(View.java:14105)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.os.Handler.handleCallback(Handler.java:605)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.os.Looper.loop(Looper.java:137)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at java.lang.reflect.Method.invoke(Method.java:511)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 06:38:40.223: E/AndroidRuntime(16702):    at dalvik.system.NativeStart.main(Native Method)

mCameragetCameraInstance()如果有帮助,则从该方法返回 null 。

4

1 回答 1

3

在您的onResume()中,您正在调用:

mCamera = Camera.open();
mCamera = getCameraInstance();

这里发生了什么,您正在通过此语句获取相机实例,Camera.open()并且在此语句之后,您再次尝试通过调用getCameraInstance();which 来获取相机实例,这将返回null因为您的活动已经获得了实例,因此 mCamera 实例正在获取null.

如果这不能解决您的问题,请告诉我,我会寻找其他解决方案。

于 2012-06-19T06:12:48.747 回答