Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:31:12+00:00 2026-06-02T06:31:12+00:00

I am trying to implement an activity which shows a camera preview and contains

  • 0

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.

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


}

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

mainscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<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" >

<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" />

<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" >
</SurfaceView>

<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" />

<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" />
 </RelativeLayout>

Edit**

Oh wow. I’m retarded. I just figured it out. I was using Button instead of ImageButton. SMH Sorry, guys. lol

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:31:14+00:00Added an answer on June 2, 2026 at 6:31 am

    Try changing TagBttn = (Button) findViewById(R.id.tag); to TagBttn = (ImageButton) findViewById(R.id.tag);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement ViewPager to combine two view within one activity, in which
I'm trying to implement multiple spinners within one activity, which appears to be working
I am trying to implement an activity similar to InvokeWorkflow, which could dynamically load
I am trying to implement a WF4 activity extension which to handle some long
I'm trying to implement some functions like file downloading which the download kickoff buttons
I am trying to implement a layout, which contains a list of ViewPagers. Each
I am trying implement a search activity which gets data from a mysql table
I am trying to implement the SVProgressHUD activity indicator. I copied the classes to
I am trying to implement the SVProgressHUD progress activity indicator. I copied the class
I'm trying to implement async on Android but it keeps crashing my app, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.