0

みなさん、おはようございます!

私は次の問題を抱えています:センサーアクセレロメッターがあり、別のインテントを入力するのに役立つボタンを追加しましたが、このボタンが機能しない理由がわかりません!!! 何か提案??助けてくれてありがとう!!

プログラムはこれです:

package com.dani;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FM extends Activity implements SensorEventListener,OnClickListener{
private static final double nbElements = 30;
private SensorManager sensorManager;
private Sensor accelerometer;
Button boton1;

   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.fm);
      boton1 = (Button) findViewById(R.id.button1);




      sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        boolean accelSupported = sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),  
     SensorManager.SENSOR_DELAY_FASTEST);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (!accelSupported) {
            sensorManager.unregisterListener(this, accelerometer);
            ((TextView) findViewById(R.id.textView1)).setText("Accelerometer not
   detected");
        }


        }  

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}
long now = 0;
long time = 0;
int temp = 0;

public void onSensorChanged(SensorEvent event) {
    long tS;
    // only if the event is from the accelerometer
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        tS = event.timestamp;

        if (now != 0) {
            temp++;
            if (temp == nbElements) {
                time = tS - now;
                ((TextView) findViewById(R.id.textView1)).setText("Frecuencia : " + 
 (nbElements * 1000000000 / time)
                        + " Hz");
                temp = 0;
            }
        }
        // To set up now on the first event and do not change it while we do not have  
 "nbElements" events
        if (temp == 0) {
            now = tS;
        }
    }


boton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    startActivity( new Intent(FM.this,PruebaActivity.class));

 }
 });
}

public void onClickQuit(View v) {
finish();
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

}

`
4

1 に答える 1

1

yuoには2つのonClickメソッドがあり、onClickListenerを実装しないので、次のようなコードを試してください。

package com.dani;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FM extends Activity implements SensorEventListener {
private static final double nbElements = 30;
private SensorManager sensorManager;
private Sensor accelerometer;
Button boton1;

   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.fm);
      boton1 = (Button) findViewById(R.id.button1);

      sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
      boolean accelSupported = sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),  
      SensorManager.SENSOR_DELAY_FASTEST);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (!accelSupported) {
            sensorManager.unregisterListener(this, accelerometer);
            ((TextView) findViewById(R.id.textView1)).setText("Accelerometer not
   detected");
        }


        }  

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}
long now = 0;
long time = 0;
int temp = 0;

public void onSensorChanged(SensorEvent event) {
    long tS;
    // only if the event is from the accelerometer
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        tS = event.timestamp;

        if (now != 0) {
            temp++;
            if (temp == nbElements) {
                time = tS - now;
                ((TextView) findViewById(R.id.textView1)).setText("Frecuencia : " + 
 (nbElements * 1000000000 / time)
                        + " Hz");
                temp = 0;
            }
        }
        // To set up now on the first event and do not change it while we do not have  
 "nbElements" events
        if (temp == 0) {
            now = tS;
        }
    }


boton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i = new Intent(FM.this, PruebaActivity.class);
    startActivity(i);   
 }
 });
}

public void onClickQuit(View v) {
finish();
}

}

AndroidManifest.xmlは次の行を追加します(com.example.stackoverflowtestingがパッケージ名ではなく、com.daniだと思います):

    <activity
        android:name="com.example.stackoverflowtesting.PruebaActivity"> 
    </activity>

PruebaActivityでこれを行います(たとえば、最初のアクティビティと同じビューを設定してテストし、後で必要なものに変更します)

public class PruebaActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fm);
    }

}

これは機能しているはずです!

于 2013-03-27T15:23:02.340 に答える