0

我正在尝试实现一个显示相机预览并包含两个按钮的活动。获取相机预览没有问题,但是当我尝试 findViewById 按钮对象时,应用程序将崩溃。不知道为什么会这样。

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


}

如果有人能引导我朝着正确的方向前进,我将不胜感激。谢谢!

主屏幕.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>

编辑**

哇哦。我是慢热型。我刚刚想通了。我使用的是 Button 而不是 ImageButton。SMH 对不起,伙计们。哈哈

4

4 回答 4

5

尝试更改TagBttn = (Button) findViewById(R.id.tag);TagBttn = (ImageButton) findViewById(R.id.tag);

于 2012-04-20T18:31:08.007 回答
2

可能是您试图查找 id 不在mainscreen.xml中

这里的标签是一个 ImageButton 没有 simole Button 试试这个

TagBttn = (ImageButton) findViewById(R.id.tag);
于 2012-04-20T18:21:09.597 回答
1

您的标签项目是一个 ImageButton 并且您正在将其转换为一个 Button。更改。ImageButton 和 Button 是非常不同的类。

于 2012-04-20T18:32:47.777 回答
1

不管它听起来多么令人困惑,事实证明它ImageButton不是Button. 因此,您希望将代码替换为:

final ImageButton TagBttn;
final ImageButton ParkBttn;

然后使用:

TagBttn = (ImageButton) findViewById(R.id.tag);
ParkBttn = (ImageButton) findViewById(R.id.park);
于 2012-04-20T18:33:02.387 回答