【问题标题】:Android camera activity is not initiatingAndroid 相机活动未启动
【发布时间】:2012-04-18 20:09:32
【问题描述】:

这是我的代码的一部分,用于一个简单的 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);

我不明白这个问题。各位大神能解释一下吗?

【问题讨论】:

  • 你能粘贴你的布局文件吗?您的 main.xml 中有 ID 为 surface_camera 的表面视图吗?

标签: android android-intent android-camera android-camera-intent


【解决方案1】:

NullPointerException 表示 mSurfaceView 是 null

在添加 OnClickListener 之前尝试添加此代码:

if (null == mSurfaceView)
  Log.e(TAG, "mSurfaceView is NULL!");
else
  Log.e(TAG, 'mSurfaceView IS OKAY!");

【讨论】:

    【解决方案2】:
    mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
    

    正在返回 null。空指针异常是因为你试图访问的指针是空的。

    See findViewById API here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      相关资源
      最近更新 更多