0

This is a part of my code for a simple android camera application,

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);

}

but when i run the program it gives null pointer exception in the following line

mSurfaceView.setOnClickListener(this);

I do not understand the problem. Can you guys clarify?

P basak
  • 4,874
  • 11
  • 40
  • 63
  • Can you paste your layout file? Do you have a surface view with ID surface_camera in your main.xml? – Anirudh Apr 18 '12 at 21:15

2 Answers2

0

The NullPointerException means that mSurfaceView is null.

Try adding this code before you add the OnClickListener:

if (null == mSurfaceView)
  Log.e(TAG, "mSurfaceView is NULL!");
else
  Log.e(TAG, 'mSurfaceView IS OKAY!");
slayton
  • 20,123
  • 10
  • 60
  • 89
0
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);

is returning null. The null pointer exception is because the pointer you are trying to access is null.

See findViewById API here

Community
  • 1
  • 1
dag
  • 2,168
  • 2
  • 16
  • 15