我正在尝试一个示例项目,用于在单击按钮时存储 android 加速度计值,它正在工作并且我得到了值,现在我想存储加速度计读数从按钮单击开始并在按钮释放时结束,我尝试使用OnTouchListener但我我无法控制加速度计,任何人之前都尝试过,任何类型的答案都会对我有帮助
layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorMgr.registerListener((SensorEventListener) getApplicationContext(), mAccelerometer,SensorManager.SENSOR_DELAY_FASTEST);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
sensorMgr.unregisterListener(this);
}
return true;
}
});
这是我用来控制加速度计的代码,但是我的应用程序会强制关闭,在 logcat 中给出错误是
03-27 12:36:29.570: I/TextToSpeech.java(28968): initTts() successfully bound to service
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 1
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 2
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 3
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 4
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 5
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 6
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 7
03-27 12:36:29.578: D/SensorManager(28968): ====>>>>>Num Sensor: 0
03-27 12:36:30.570: W/dalvikvm(28968): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-27 12:36:30.578: E/AndroidRuntime(28968): FATAL EXCEPTION: main
03-27 12:36:30.578: E/AndroidRuntime(28968): java.lang.ClassCastException: android.app.Application
03-27 12:36:30.578: E/AndroidRuntime(28968): at com.example.androidphonemotion.MainActivity$1.onTouch(MainActivity.java:59)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.view.View.dispatchTouchEvent(View.java:3934)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-27 12:36:30.578: E/AndroidRuntime(28968): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1733)
03-27 12:36:30.578: E/AndroidRuntime(28968): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1151)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-27 12:36:30.578: E/AndroidRuntime(28968): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1717)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2215)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.view.ViewRoot.handleMessage(ViewRoot.java:1886)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.os.Looper.loop(Looper.java:130)
03-27 12:36:30.578: E/AndroidRuntime(28968): at android.app.ActivityThread.main(ActivityThread.java:3689)
03-27 12:36:30.578: E/AndroidRuntime(28968): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 12:36:30.578: E/AndroidRuntime(28968): at java.lang.reflect.Method.invoke(Method.java:507)
03-27 12:36:30.578: E/AndroidRuntime(28968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-27 12:36:30.578: E/AndroidRuntime(28968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-27 12:36:30.578: E/AndroidRuntime(28968): at dalvik.system.NativeStart.main(Native Method)