我正试着把我的加速计类嵌入到我的第二个活动中,但是我总是得到一个强制转换异常
这是我的加速计课程:
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;
@Override
public 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.SensorEventListener
01-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.access$900(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
对象作为参数,而不是在SensorEventListener
中实现SecondActivity
。
因为您在SensorEventListener
中实现Accelerometer
,所以将acc
作为registerListener中的第一个参数传递:
sensorManager.registerListener((SensorEventListener)acc, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);