0

這是我的一個簡單的Android攝像頭應用程序代碼的一部分,Android攝像頭活動沒有啓動

public class CameraView extends Activity implements Callback, OnClickListener { 


static final int FOTO_MODE = 0; 
private static final String TAG = "CameraTest"; 
Camera mCamera; 
boolean mPreviewRunning = false; 
private Context mContext = this; 

public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 

    Log.e(TAG, "onCreate"); 

    Bundle extras = getIntent().getExtras(); 

    getWindow().setFormat(PixelFormat.TRANSLUCENT); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.main); 
    mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera); 
    mSurfaceView.setOnClickListener(this); 
    mSurfaceHolder = mSurfaceView.getHolder(); 
    mSurfaceHolder.addCallback(this); 
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
} 

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { 
    public void onPictureTaken(byte[] imageData, Camera c) { 

     if (imageData != null) { 

      Intent mIntent = new Intent(); 

      StoreByteImage(mContext, imageData, 50, 
        "ImageName"); 
      mCamera.startPreview(); 

      setResult(FOTO_MODE, mIntent); 
      finish(); 

     } 
    } 
}; 







public void onClick(View arg0) { 

    mCamera.takePicture(null, mPictureCallback, mPictureCallback); 

} 

但是當我運行它,它給出了下面的行空指針異常

mSurfaceView.setOnClickListener(this); 

我不明白這個問題。你們能澄清一下嗎?

+0

你能粘貼你的佈局文件嗎?你在main.xml中有ID surface_camera的表面視圖嗎? – Anirudh 2012-04-18 21:15:54

回答

0

NullPointerException表示mSurfaceView爲null

嘗試添加該代碼添加了OnClickListener前:

if (null == mSurfaceView) 
    Log.e(TAG, "mSurfaceView is NULL!"); 
else 
    Log.e(TAG, 'mSurfaceView IS OKAY!"); 
0
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera); 

被返回null。空指針異常是因爲您嘗試訪問的指針爲空。

See findViewById API here