0

I'm currently using a custom camera, however when I choose to select a image from the gallery and I press back and then continue to take a picture, it crashes on a nullpointer in the mCamera instance.

When I open the gallery I choose to release the camera, this works. After that when I press back, it comes in the activity for result. From there I manually call the method that creates all the instances the camera needs. However when calling the mCamera.open it gives an error, which I catch. But when I want to take a picture it crashes because the mCamera is a null.

What am I doing wrong here? I release the camera so it should be able to get a new one.

Edit:

Release:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 100);

        try {
            mCamera.release();
            Log.e("Camera", "Camera has been released");
        } catch (NullPointerException E){
            E.printStackTrace();
        }

Create:

// Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);
Jordi Sipkens
  • 595
  • 1
  • 9
  • 25

2 Answers2

0

Correct solution:

http://www.stackoverflow.com/a/19312951/867591

Thank you Ahmed!

"It is right, you have open your camera in the onStart method and release it in the onStop method of your activity. The methods onResume and onPause are part of the visible lifecycle of the android activity. OnStop is called when another activity occupies the whole visible space. OnPause is even called when another activity comes to the foreground even if it does not occupy the whole visible space such as the intent chooser dialog does when it pops up. So I guess moving your camera creation and release into the correct lifecycle methods should do the trick. You can find further information about the activity lifecycle here, but I'm sure you are familiar with that:

http://developer.android.com/reference/android/app/Activity.html"

What did I do wrong? I actually did all of my creating stuff in the onResume, however switching it to the onStart fixed the issue.

Jordi Sipkens
  • 595
  • 1
  • 9
  • 25
0

make sure that you're calling the getCameraInstance() method in onCreate() method,because if you're calling it outside the onCreate() method the camera will not be enabled and will give u a null pointer exception.

I think this link can help you out:

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40