0

カメラのプレビューを表示し、2 つのボタンを含むアクティビティを実装しようとしています。カメラのプレビューを取得することは問題ありませんが、ボタン オブジェクトに対して 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>

編集**

ああすごい。私は遅れています。私はちょうどそれを理解しました。ImageButton の代わりに Button を使用していました。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 ボタンはありません これを試してください

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 に答える