0

I have developed a basic Android application which simply displays the phone's camera view in a VideoView element. I have added camera access permission in the Manifest

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

And the application consists of the main activity MainActivity.java only. A snap of the code will be provided in the next few lines. However I face a problem when running the apk file on an actual device: An error is displayed stating that the application has stopped unexpectedly and I have no idea why.

This is the code for the main activity

public class MainActivity extends Activity implements SurfaceHolder.Callback {

    private Button StartButton = null;
    private VideoView videoView = null;
    private SurfaceHolder holder = null;
    private Camera camera = null;
    private static final String TAG = "Video";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StartButton = (Button) findViewById(R.id.StartButton);
        videoView = (VideoView) this.findViewById(R.id.videoView);

        StartButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try{
                    camera = Camera.open();
                    Camera.Parameters camParams = camera.getParameters();
                    camera.lock();

                } catch(RuntimeException re){
                    Log.v(TAG, "Could not initialize the Camera");
                    re.printStackTrace();
                }
            }
        });

        holder = videoView.getHolder();
        holder.addCallback(this);
    }


    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }


    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        Log.v(TAG, "in surfaceCreated");

        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        } catch(IOException e) {
            Log.v(TAG, "Could not start the preview");
            e.printStackTrace();
        }
    }

The xml file basically contains two elements: the VideoView and a Start button. Any idea what stops the application unexpectedly? Is there a better way to implement this concept? Having the Start button is not necessary in my case. In fact it would be better to do this without a Start button.

I am very new to Android development and java coding. I am using Eclipse adt-bundle for Android development.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
shallawati
  • 145
  • 1
  • 3
  • 10

1 Answers1

0

The problem is that you only set a value for the camera member when the button is clicked, but try to use camera much earlier, when the VideoView's holder surface is created. Therefore, a NullPointerException is thrown, and your application crashes.

It's important to realize that the VideoView and its surface are created as part of the activity's layout, along with the button, so this obviously happens before the user has the chance to click anything.

If the button were really necessary, you could fix this by building the VideoView programmatically at some later point, after the button's already been clicked. Since you mention you don't particularly want the button, however, there's an even simpler solution: get rid of it, and move the camera initialization to onCreate itself.

The result might look something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        camera = Camera.open();
        camera.lock();
    } catch(RuntimeException re) {
        Log.v(TAG, "Could not initialize the Camera");
        re.printStackTrace();
    }

    videoView = (VideoView) this.findViewById(R.id.videoView);
    holder = videoView.getHolder();
    holder.addCallback(this);
}

There are some other problems with the code you put up (e.g. no call to camera.release() means any attempt to so much as rotate the device will cause the application to crash), but the code with this fix will at least allow the preview to start.

I would, however, suggest looking into Android code conventions (it seems you don't even stick to any one set of conventions for the few lines you showed), and into how to debug using real devices, which could help you find the cause for crashes such as this one yourself.

Good luck!

Tomty
  • 1,962
  • 1
  • 22
  • 37