好吧,我根本没有别的方法可以解决这个问题。我有一个看起来如下的功能:
@Override
public void onSensorChanged(SensorEvent event) {
old_orientation = orientation;
if(event.sensor == grav) {
last_grav_reading = event.values;
} else {
last_magnet_reading = event.values;
}
if (last_grav_reading != null && last_magnet_reading != null) {
sensorManager.getRotationMatrix(mat_rotation, mat_inclination, last_grav_reading, last_magnet_reading);
sensorManager.getOrientation(mat_rotation, orientation);
if (listener != null) {
listener.onOrientationSensorUpdate(old_orientation, orientation);
}
}
}
所以" getRotationMatrix"应该只在last_magnet_reading和last_grav_reading都为非零时执行吗?
再次猜测,该行实际执行(并导致严重崩溃),一张显示案例的图片(所选行是调试器报告执行的行,下面是您可以看到每个变量的值): / p>
那么......发生了什么? JRE是否在Android上出现过漏洞?哦,没有其他功能/线程可以访问这些变量。此外,它不会发生在我的主电话(Android 5.0.1)上,但它确实发生在我的辅助连接(4.3)上。 (我可以"期待"传感器根本不存在,但仍然会违反if语句。)
编辑,全班:
public class OrientationTracker implements SensorEventListener, IOrientationTracker {
private SensorManager sensorManager;
private Sensor magnet;
private Sensor grav;
private float[] last_grav_reading;
private float[] last_magnet_reading;
private float[] mat_inclination = new float[9];
private float[] mat_rotation = new float[9];
private float[] old_orientation;
private float[] orientation;
private boolean isRunning;
private OrientationUpdateListener listener;
public OrientationTracker(Context context) {
sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
this.magnet = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
this.grav = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
this.orientation = new float[]{0,0,0};
isRunning = false;
if (this.magnet != null &&this.grav != null){
// Success! There's a magnetometer.
}
}
public void start(){
if(isRunning) {
//Already running, do nothing
return;
}
registerSensors();
isRunning = true;
}
public void start(OrientationUpdateListener listener) {
start();
this.listener = listener;
}
public void stop() {
unregisterSensors();
isRunning = false;
}
public void registerSensors() {
sensorManager.registerListener(this, magnet, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, grav, SensorManager.SENSOR_DELAY_NORMAL);
}
public void unregisterSensors() {
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
old_orientation = orientation;
String t = event.sensor.getName();
String p = grav.getName();
if(event.sensor.equals(grav)) {
last_grav_reading = event.values;
} else {
last_magnet_reading = event.values;
}
if (last_grav_reading != null && last_magnet_reading != null) {
sensorManager.getRotationMatrix(mat_rotation, mat_inclination, last_grav_reading, last_magnet_reading);
sensorManager.getOrientation(mat_rotation, orientation);
if (listener != null) {
listener.onOrientationSensorUpdate(old_orientation, orientation);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
跟踪:
"<1> main@830026754464" prio=5 runnable
java.lang.Thread.State: RUNNABLE
at com.scoutingstuff.paul.ivossenjacht.OrientationTracker.onSensorChanged(OrientationTracker.java:71)
at android.hardware.SystemSensorManager$ListenerDelegate$1.handleMessage(SystemSensorManager.java:204)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Method.java:-1)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java:-1)
"<12> android.hardware.SystemSensorManager$SensorThread@830035660424" prio=5 runnable
java.lang.Thread.State: RUNNABLE
Incompatible thread state: thread not suspended
"<11> GLThread 357@830035646696" prio=5 waiting
java.lang.Thread.State: WAITING
Incompatible thread state: thread not suspended
"<10> Binder_2@830035587352" prio=5 runnable
java.lang.Thread.State: RUNNABLE
Incompatible thread state: thread not suspended
"<9> Binder_1@830035581424" prio=5 runnable
java.lang.Thread.State: RUNNABLE
Incompatible thread state: thread not suspended
"<8> FinalizerWatchdogDaemon@830035565608" daemon prio=5 waiting
java.lang.Thread.State: WAITING
Incompatible thread state: thread not suspended
"<7> FinalizerDaemon@830035565176" daemon prio=5 waiting
java.lang.Thread.State: WAITING
Incompatible thread state: thread not suspended
"<6> ReferenceQueueDaemon@830035564816" daemon prio=5 waiting
java.lang.Thread.State: WAITING
Incompatible thread state: thread not suspended
"<5> Compiler@830035564576" daemon prio=5 waiting
java.lang.Thread.State: WAITING
Incompatible thread state: thread not suspended
"<3> Signal Catcher@830035564096" daemon prio=5 waiting
java.lang.Thread.State: WAITING
Incompatible thread state: thread not suspended
"<2> GC@830035563872" daemon prio=5 waiting
java.lang.Thread.State: WAITING
Incompatible thread state: thread not suspended
答案 0 :(得分:0)
(last_grav_reading != null && last_magnet_reading != null)
收到重力事件后,以及收到磁铁事件后,为真。没有代码将last_XXX_reading返回null。你的意思是
if(event.sensor == grav) {
last_magnet_reading = null;
last_grav_reading = event.values;
} else {
last_grav_reading = null;
last_magnet_reading = event.values;
}
两个传感器都附加到同一个侦听器this
sensorManager.registerListener(this, magnet, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, grav, SensorManager.SENSOR_DELAY_NORMAL);
答案 1 :(得分:-1)
第一个问题是什么返回event.value
?
如果它返回例如。如果由于last_grav_reading != null
总是正确的,那么如果法规将全部执行,则返回null数组。