【问题标题】:.setPreviewDisplay(holder) throwing null pointer exception.setPreviewDisplay(holder) 抛出空指针异常
【发布时间】:2012-01-24 09:33:20
【问题描述】:

我正在开发一个自定义相机应用程序。下面是我的 Activity 类。

public class MyCustomCam extends Activity {
private Camera mCamera;    
private CameraPreview mPreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mCamera = getCameraInstance();       
    mPreview = new CameraPreview(this, mCamera);        
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);       
    preview.addView(mPreview);
}
    public static Camera getCameraInstance(){    
        Camera c = null;    
        try {
        c = Camera.open(); // attempt to get a Camera instance    
        }   
        catch (Exception e){        
            // Camera is not available (in use or does not exist)    
            }    
        return c; // returns null if camera is unavailable}

       }
       }

下面是我的 main.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical"    
  android:layout_width="fill_parent"   
   android:layout_height="fill_parent"    > 
 <FrameLayout 
   android:id="@+id/camera_preview"    
   android:layout_width="fill_parent"
       android:layout_height="fill_parent" 
          android:layout_weight="1"    /> 
        <Button    android:id="@+id/button_capture"  
          android:text="Capture"   
         android:layout_width="wrap_content"    
   android:layout_height="wrap_content"    
    android:layout_gravity="center" 
   /></LinearLayout>

这是我的 CameraPreview 类

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {    
private SurfaceHolder mHolder;    
private Camera mCamera;    
public CameraPreview(Context context, Camera camera) {        
    super(context);        
    mCamera = camera;        // Install a SurfaceHolder.Callback so we get notified when the       
    // underlying surface is created and destroyed.        
    mHolder = getHolder();       
    mHolder.addCallback(this); 
    // deprecated setting, but required on Android versions prior to 3.0        
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
    }   
public void surfaceCreated(SurfaceHolder holder) {       
    // The Surface has been created, now tell the camera where to draw the preview.        
    try {            
        mCamera.setPreviewDisplay(holder);            
        mCamera.startPreview();        
    } catch (IOException e) {            

        Log.d("", "Error setting camera preview: " + e.getMessage());       
        }   
    } 
public void surfaceDestroyed(SurfaceHolder holder) {
            // empty. Take care of releasing the Camera preview in your activity.   
    }    
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.        
        // Make sure to stop the preview before resizing or reformatting it.        
        if (mHolder.getSurface() == null){          // preview surface does not exist         
            return;        }        // stop preview before making changes        
        try {            
            mCamera.stopPreview();    
        } catch (Exception e){    
            // ignore: tried to stop a non-existent preview      
            }        // set preview size and make any resize, rotate or 
        // reformatting changes here        // start preview with new settings   
        try {            
            mCamera.setPreviewDisplay(mHolder);         
            mCamera.startPreview();       
        } catch (Exception e){           
            Log.d("", "Error starting camera preview: " + e.getMessage()); 
           }  
}
   }

这里

   mCamera.setPreviewDisplay(holder); 

代码抛出空指针异常...我无法修复它。请告诉我为什么它抛出异常以及如何修复它?

【问题讨论】:

  • 我有同样的错误。你是怎么解决这个问题的。你能指导一下吗?

标签: android nullpointerexception android-camera surfaceholder


【解决方案1】:

在你的surfaceDestroyed函数中释放相机

public void surfaceDestroyed(SurfaceHolder holder) {
        if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;   
} 

并确保在清单文件中添加相机权限。

<uses-permission android:name="android.permission.CAMERA" />

【讨论】:

    【解决方案2】:

    我遇到了同样的错误。

    我使用此问答来帮助解决我的问题。具体来说,请参考在surfaceChanged中设置params.setPreviewSize(...)之前调用的函数getBestPreviewSize(...)的添加。

    Android - cam.setPreviewDisplay(holder) running into IOError

    附带说明(因为这是我的下一个错误),如果您开始对布局进行更多自定义(例如删除标题栏),则应在 setContentView 之前对 UI 进行任何“请求”以进行更改(...),在下面的链接中注明。

    requestFeature() must be called before adding content

    【讨论】:

    • 你刚刚成为我的个人英雄,该解决方案帮助我前进,在官方文档中没有提及这一点,除了一行说选择最佳预览尺寸。再次感谢您
    【解决方案3】:

    您的问题很可能是您的模拟器的相机设置。去 AVD 管理器,编辑你的模拟器并设置你的相机,可能是模拟的。重新启动模拟器并启动您的应用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-22
      • 2020-08-10
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多