我正在尝试制作一个应用程序,它可以向我显示我的设备正在旋转的角度,并且还有一个菜单可以让我选择以度数或弧度查看结果,但是在运行此代码后,仅出现显示布局的屏幕并且在旋转设备时什么都没有发生请提前告诉我我错在哪里谢谢
package com.example.directordetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_name, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.degree:
m = 0;
return true;
case R.id.radian:
m = 1;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
TextView tvX = (TextView) findViewById(R.id.x_axis);
TextView tvY = (TextView) findViewById(R.id.y_axis);
TextView tvZ = (TextView) findViewById(R.id.z_axis);
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if (!mInitialized) {
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText("0.0");
tvY.setText("0.0");
tvZ.setText("0.0");
mInitialized = true;
} else {
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
if (deltaX < NOISE)
deltaX = (float) 0.0;
if (deltaY < NOISE)
deltaY = (float) 0.0;
if (deltaZ < NOISE)
deltaZ = (float) 0.0;
mLastX = x;
mLastY = y;
mLastZ = z;
if (m == 1) {
deltaX = (float) (deltaX * (3.18 / 180));
deltaX = (float) (deltaX * (3.18 / 180));
deltaX = (float) (deltaX * (3.18 / 180));
}
tvX.setText(Float.toString(deltaX));
tvY.setText(Float.toString(deltaY));
tvZ.setText(Float.toString(deltaZ));
}
}
}