Android SensorEventListener 语法
我已经做好了被羞辱的准备,但我不知道哪里出了问题。我只是在玩 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;
}
}
感谢您的帮助。这是我的参考: 参考项目
I am prepared to be humiliated on this, but I am not sure what is wrong. I am just playing around with Android development and am just making a simple application that will print out accelerometer to a TextView.
When creating the SensorEventListener, I get a syntax error
(says ; is needed to finish statement)
exact code in question:
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);
}
}
}
}
here is full code if needed
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);
}
}
and here is the activity:
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;
}
}
thanks for any help. this is my reference: reference project
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像它说的,你错过了一个 ;
Like it says, you are missing a ;