简体   繁体   中英

I'm unable to see why I'm getting a NullPointerException

First off, I'm a beginner to this whole Android party. I'm attempting to write code to start and stop a camera preview on the click of a button. I would eventually like to extract individual frames from the preview but I'm running into issues with getting the preview to start correctly.

Below is my code. My camera preview is to start/stop on the click of the launchFrames button. A boolean value that is toggled true/false every time the button tells the onClickListener whether to call the camera_start() or camera_stop() method. At the moment, my app crashes right after the button is created (and before the onClickListener is implemented).

I identified this is where it is crashing by adding Log messages as can be seen in the LogCat attached. Please shed some light on what is causing this error and also any other issues you may notice with my code (general structure or specific things). Let me know if I've failed to provide enough information about my problem.

public class MainActivity extends ActionBarActivity implements SurfaceHolder.Callback{

public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public static final int REQUEST_VIDEO_CAPTURE = 1;
public static final String TAG = "MAIN";
private static boolean toggleFrameLauncher = false;
Button launchFrames;
Camera mcamera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "Started onCreate method"); ////////// DEBUG ///////////
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(TAG, "setContentView executed successfully"); /////// DEBUG //////

    Button launchFrames = (Button)findViewById(R.id.button_frames);
    Log.i(TAG, "Created button reference successfully");//// DEBUG ////////

    launchFrames.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (!toggleFrameLauncher) {
                toggleFrameLauncher = !toggleFrameLauncher;
                start_camera();
            } else {
                toggleFrameLauncher = !toggleFrameLauncher;
                stop_camera();
            }
        }
    });

    Log.i(TAG, "Button configured correctly"); /DEBUG-Code does not reach here/
    surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
        .add(R.id.container, new PlaceholderFragment())
        .commit();
    }
}

public void start_camera() {

    try {
        mcamera = Camera.open();
    } catch(RuntimeException e) {
        Log.e(TAG, "init_camera: " + e);
        return;
    }
    Camera.Parameters param;
    param = mcamera.getParameters();
    //modify parameter
    //param.setPreviewFrameRate(20);
    param.setPreviewSize(480,320);
    param.setPreviewFormat(ImageFormat.RGB_565);
    mcamera.setParameters(param);
    try {
        mcamera.setPreviewDisplay(surfaceHolder);
        mcamera.startPreview();
    } catch (Exception e) {
        Log.e(TAG, "init_camera: " + e);
        return;
    }
}

private void stop_camera() {
    mcamera.stopPreview();
    mcamera.release();
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    if(previewing) {
        mcamera.stopPreview();
        previewing = false;
    }

    if (mcamera != null) {
        try {
            mcamera.setPreviewDisplay(surfaceHolder);
            mcamera.startPreview();
            previewing = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    mcamera = Camera.open();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mcamera.stopPreview();
    mcamera.release();
    mcamera = null;
    previewing = false;
}

}

Here is my LogCat:

05-24 18:58:33.463: I/MAIN(19366): Started onCreate method
05-24 18:58:33.503: I/MAIN(19366): setContentView executed successfully
05-24 18:58:33.503: I/MAIN(19366): Created button reference successfully
05-24 18:58:33.503: W/dalvikvm(19366): threadid=1: thread exiting with uncaught exception (group=0x4153aba8)
05-24 18:58:33.503: E/AndroidRuntime(19366): FATAL EXCEPTION: main
05-24 18:58:33.503: E/AndroidRuntime(19366): Process: com.example.myfirstapp, PID: 19366
05-24 18:58:33.503: E/AndroidRuntime(19366): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.NullPointerException
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.os.Looper.loop(Looper.java:136)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.app.ActivityThread.main(ActivityThread.java:5017)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at java.lang.reflect.Method.invokeNative(Native Method)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at java.lang.reflect.Method.invoke(Method.java:515)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at dalvik.system.NativeStart.main(Native Method)
05-24 18:58:33.503: E/AndroidRuntime(19366): Caused by: java.lang.NullPointerException
05-24 18:58:33.503: E/AndroidRuntime(19366):    at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:46)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.app.Activity.performCreate(Activity.java:5231)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-24 18:58:33.503: E/AndroidRuntime(19366):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-24 18:58:33.503: E/AndroidRuntime(19366):    ... 11 more

And here is the XML file for my activity:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation = "horizontal">

    <Button
        android:id="@+id/button_frames"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_box_1"
        android:layout_toRightOf = "@id/button_video"
        android:text="@string/button_frames" />

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

Given your log messages, launchFrames must be null . You would be able to tell that more directly by noticing that the stack trace tells you the exact line number where you are crashing.

Try cleaning your project (eg, Project > Clean in the Eclipse main menu) and see if that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM