我试图将我的加速度计类嵌入我的secondeActivity中,但是我总是遇到强制异常
这是我的加速度计课:
package com.example.lincision;import androID.harDWare.Sensor;import androID.harDWare.SensorEvent;import androID.harDWare.SensorEventListener;public class Accelerometer implements SensorEventListener { private float xAxis; private float yAxis; private float zAxis; private float lastX, lastY, lastZ; public float getX() { return this.xAxis; } public float getY() { return this.yAxis; } public float getZ() { return this.zAxis; } @OverrIDe public voID onAccuracyChanged(Sensor sensor, int accuracy) { } @OverrIDe public voID onSensorChanged(SensorEvent event) { // get the change of the x,y,z values of the accelerometer xAxis = Math.abs(lastX - event.values[0]); yAxis = Math.abs(lastY - event.values[1]); zAxis = Math.abs(lastZ - event.values[2]); }}
在这里,您看到我的第二活动根本没有用,也许是我的主意
必须改变.
package com.example.lincision;import androID.app.Activity;import androID.content.Context;import androID.harDWare.Sensor;import androID.harDWare.SensorEventListener;import androID.harDWare.SensorManager;import androID.os.Bundle;import androID.Widget.TextVIEw;import com.javacodegeeks.androIDaccelerometerexample.R;public class SecondActivity extends Activity { TextVIEw outputX; TextVIEw outputY; TextVIEw outputZ; SensorManager manager; Sensor accelerometer; SensorManager sensorManager; TextVIEw currentX, currentY, currentZ; @OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.secondactivity); initializeVIEws(); displayCleanValues(); // display the current x,y,z accelerometer values displayCurrentValues(); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); accelerometer = sensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener((SensorEventListener) acc, accelerometer, SensorManager.SENSOR_DELAY_norMAL);} Accelerometer acc = new Accelerometer(); public voID initializeVIEws() { currentX = (TextVIEw) findVIEwByID(R.ID.currentX); currentY = (TextVIEw) findVIEwByID(R.ID.currentY); currentZ = (TextVIEw) findVIEwByID(R.ID.currentZ); } private float deltaX = acc.getX(); private float deltaY = acc.getY(); private float deltaZ = acc.getZ(); public voID displayCleanValues() { currentX.setText("0.0"); currentY.setText("0.0"); currentZ.setText("0.0"); } // display the current x,y,z accelerometer values public voID displayCurrentValues() { currentX.setText(float.toString(deltaX)); currentY.setText(float.toString(deltaY)); currentZ.setText(float.toString(deltaZ)); }}
这是我的错误信息:
01-14 12:36:58.751: E/AndroIDRuntime(18696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.javacodegeeks.androIDaccelerometerexample/com.example.lincision.SecondActivity}: java.lang.classCastException: com.example.lincision.SecondActivity cannot be cast to androID.harDWare.SensorEventListener01-14 12:36:58.751: E/AndroIDRuntime(18696): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2394)01-14 12:36:58.751: E/AndroIDRuntime(18696): at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)01-14 12:36:58.751: E/AndroIDRuntime(18696): at androID.app.ActivityThread.access0(ActivityThread.java:172)01-14 12:36:58.751: E/AndroIDRuntime(18696): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)01-14 12:36:58.751: E/AndroIDRuntime(18696): at androID.os.Handler.dispatchMessage(Handler.java:102)01-14 12:36:58.751: E/AndroIDRuntime(18696): at androID.os.Looper.loop(Looper.java:136)01-14 12:36:58.751: E/AndroIDRuntime(18696): at androID.app.ActivityThread.main(ActivityThread.java:5586)01-14 12:36:58.751: E/AndroIDRuntime(18696): at java.lang.reflect.Method.invokeNative(Native Method)01-14 12:36:58.751: E/AndroIDRuntime(18696): at java.lang.reflect.Method.invoke(Method.java:515)01-14 12:36:58.751: E/AndroIDRuntime(18696): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)01-14 12:36:58.751: E/AndroIDRuntime(18696): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:1084)01-14 12:36:58.751: E/AndroIDRuntime(18696): at dalvik.system.NativeStart.main(Native Method)01-14 12:36:58.751: E/AndroIDRuntime(18696): Caused by: java.lang.classCastException: com.example.lincision.SecondActivity cannot be cast to androID.harDWare.SensorEventListener
解决方法:
SensorManager.registerListener将SensorEventListener对象作为参数,并且您没有在SecondActivity中实现SensorEventListener.
因为您要在Accelerometer中实现SensorEventListener,所以将acc作为第一个参数传递给registerListener:
sensorManager.registerListener((SensorEventListener)acc, accelerometer, SensorManager.SENSOR_DELAY_norMAL);
总结 以上是内存溢出为你收集整理的加速度计类的Android问题全部内容,希望文章能够帮你解决加速度计类的Android问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)