findViewById causing crash. Why?
<p>I am trying to implement an activity which shows a camera preview and contains two buttons. Getting the camera preview is no problem but when I try findViewById for the button objects the app will crash. Not sure why that's happening.</p>

<pre><code>package com.capstone.parking.nyc;

import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.graphics.PixelFormat    ;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorManager;

public class MainScreen extends Activity implements SurfaceHolder.Callback 
{
Camera theCamera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean preview = false;

private SensorManager mSensorManager; 
private ShakeListener mSensorListener;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    final Button TagBttn;
    final Button ParkBttn;
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFormat(PixelFormat.UNKNOWN);
    setContentView(R.layout.mainscreen);


         /*
          *
          * This line causes the crash
          */
    TagBttn = (Button) findViewById(R.id.tag);
//      ParkBttn = (Button)findViewById(R.id.park);



    surfaceView = (SurfaceView) findViewById(R.id.camera);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    mSensorListener = new ShakeListener();
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);     


    Log.d("TAG", "onCreate MainScreen");

    mSensorListener.setOnShakeListener(new ShakeListener.OnShakeListener()
    {
          public void onShake()
          {
              Log.d("SHAKE CHECK", "YUSSSSSS");
            // if shaken, go to the search screen 
              startActivity(new Intent("com.capstone.parking.SEARCH")); 
          }
    });


/*  Tag.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            /*
             * 
             *      ENTER TAG CODE HERE
             *                          
             *
            Log.d("TAG", "tag button pressed");
        }
    });
/*  

/*  Park.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            /*
             * 
             *      ENTER PARK CODE HERE
             * 
             *
            Log.e("TAG", "park button pressed");
        }
    });
*/  

}

@Override
public void onResume()
{
    super.onResume();
    mSensorManager.registerListener(mSensorListener,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);
}

@Override
public void onStop()
{
   mSensorManager.unregisterListener(mSensorListener);
   super.onStop();
}

public void surfaceCreated(SurfaceHolder holder) 
{
    Log.e("TAG", "surfaceCreated");
    theCamera = Camera.open();
    try 
    {
        theCamera.setPreviewDisplay(holder);
    } 
    catch (IOException e) 
    {
        Log.e("TAG", "surfaceCreated FAIL");
    }
    theCamera.startPreview();
    preview = true;
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
{
    Log.e("TAG", "surfaceChanged");
    if(preview)
    {
        theCamera.stopPreview();
    }
    Camera.Parameters parameters = theCamera.getParameters();
    parameters.setPreviewSize(width, height);
//  parameters.set("orientation", "portrait");
//  parameters.set("rotation", "90");
    theCamera.setParameters(parameters);
    theCamera.startPreview();
}

public void surfaceDestroyed(SurfaceHolder holder) 
{
    if(preview)
    {
        Log.e("TAG", "surfaceDestroyed");
        theCamera.stopPreview();
        theCamera.release();
        theCamera = null;
        preview = false;
    }
} 


}
</code></pre>

<p>If anyone could guide me in the right direction I would greatly appreciate it. Thanks!</p>

<p>mainscreen.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#006699" &gt;

&lt;TextView
    android:id="@+id/scroll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="@string/shake"
    android:textColor="#ffff66"
    android:textStyle="bold" /&gt;

&lt;SurfaceView
   android:id="@+id/camera"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_marginTop="25dp"
   android:layout_marginBottom="125dp"
   android:layout_marginLeft="20dp"
   android:layout_marginRight="20dp" &gt;
&lt;/SurfaceView&gt;

&lt;ImageButton
   android:id="@+id/tag"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignParentRight="true"
   android:layout_marginRight="25dp"
   android:layout_marginBottom="50dp"
   android:contentDescription="@string/desc"
   android:background="@drawable/tagbuttonselect"
   android:clickable="true" /&gt;

&lt;ImageButton
   android:id="@+id/park"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:layout_marginLeft="25dp"
   android:layout_marginBottom="50dp"
   android:contentDescription="@string/desc"
   android:background="@drawable/parkbuttonselect"
   android:clickable="true" /&gt;
 &lt;/RelativeLayout&gt;
</code></pre>

<p>Edit**</p>

<p>Oh wow. I'm retarded. I just figured it out. I was using Button instead of ImageButton. SMH Sorry, guys. lol</p>
