3

我准备在这件事上受到羞辱,但我不知道出了什么问题。我只是在玩 Android 开发,只是在制作一个简单的应用程序,它将加速度计打印到 TextView。

创建 SensorEventListener 时,出现语法错误

(说 ; 是完成语句所必需的)

有问题的确切代码:

    private SensorEventListener listener = new SensorEventListener() {
    public void onSensorChanged(SensorEvent e) {
        if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
            xAxis = e.values[0];
            yAxis = e.values[1];
            zAxis = e.values[2];


            if (xThreshold > xAxis || yThreshold > yAxis || zThreshold > zAxis) {
                alertUser(values);
            }
            else {
                nonAlert(values);
            }
        }
    }
}

如果需要,这里是完整的代码

    import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class TiltMeter {
    private SensorManager mgr = null;
    private double xAxis = 0;
    private double yAxis = 0;
    private double zAxis = 0;
    private double xThreshold = 0;
    private double yThreshold = 0;
    private double zThreshold = 0;

    public TiltMeter(Context context, double xThreshold, double yThreshold, double zThreshold, TiltMeter.Callback cb) {
        this.xThreshold = xThreshold;
        this.yThreshold = yThreshold;
        this.zThreshold = zThreshold;


        mgr = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        mgr.registerListener(listener, mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
    }

    private SensorEventListener listener = new SensorEventListener() {
        public void onSensorChanged(SensorEvent e) {
            if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
                xAxis = e.values[0];
                yAxis = e.values[1];
                zAxis = e.values[2];


                if (xThreshold > xAxis || yThreshold > yAxis || zThreshold > zAxis) {
                    alertUser(values);
                }
                else {
                    nonAlert(values);
                }
            }
        }
    }

    public void close() {
        mgr.unregisterListener(listener);
    }

    public interface Callback {
        double[] alertUser(double[] data);
        double[] nonAlert(double[] data);
    }



}

这是活动:

   import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TiltMeterActivity extends Activity  
    implements TiltMeter.Callback {
    /** Called when the activity is first created. */

    TiltMeter tiltMeter = null;

    TextView tvXAxis;
    TextView tvYAxis;
    TextView tvZAxis;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        tvXAxis = (TextView)findViewById(R.id.txtXAxisValue);
        tvYAxis = (TextView)findViewById(R.id.txtYAxisValue);
        tvZAxis = (TextView)findViewById(R.id.txtZAxisValue);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        tiltMeter.close();
    }


    public double[] alertUser(double[] data) {
        tvXAxis.setText(Double.toString(data[0]));
        tvYAxis.setText(Double.toString(data[1]));
        tvZAxis.setText(Double.toString(data[3]));
        return null;
    }

    public double[] nonAlert(double[] data) {
        tvXAxis.setText(Double.toString(data[0]));
        tvYAxis.setText(Double.toString(data[1]));
        tvZAxis.setText(Double.toString(data[3]));
        return null;
    }

}

谢谢你的帮助。这是我的参考: 参考项目

4

1 回答 1

2

就像它说的那样,您缺少一个 ;

private SensorEventListener listener = new SensorEventListener() {
   public void onSensorChanged(SensorEvent e) {
     if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
        xAxis = e.values[0];
        yAxis = e.values[1];
        zAxis = e.values[2];


        if (xThreshold > xAxis || yThreshold > yAxis || zThreshold > zAxis) {
            alertUser(values);
        }
        else {
            nonAlert(values);
        }
     }
   }
};
于 2010-11-08T22:26:01.777 回答