我想在我的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;
@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
答案 0 :(得分:2)
SensorManager.registerListener
将SensorEventListener
个对象作为参数,而您未在SensorEventListener
中实施SecondActivity
。
因为您正在SensorEventListener
中实现Accelerometer
,所以将acc
作为registerListener中的第一个参数传递:
sensorManager.registerListener((SensorEventListener)acc, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
答案 1 :(得分:1)
引起:java.lang.ClassCastException: com.example.lincision.SecondActivity无法强制转换为 android.hardware.SensorEventListener
如错误所示,您正在将Activity转换为SensorEventListener。这是导致问题的一行:
sensorManager.registerListener((SensorEventListener) this, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
请勿在registerListener中将此强制转换为SensorEventListener,因为此指的是SecondActivity,并且它没有实现SensorEventListener。相反,传递 acc ,这是Accelerometer实现SensorEventListener的对象,而SensorEventListener又是registerListener所需的第一个参数:
sensorManager.registerListener((SensorEventListener)acc, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);