Android executes an if statement even though it shouldn’t?
Well, I
just don’t have another way to express this, I have a function like this:
@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);
}
}
}
So “getRotationMatrix” should only be executed when neither last_magnet_reading nor last_grav_reading is zero, right?
Guess again, the line
was actually executed (and caused a serious crash), showing a picture of the case (the selected line is the line where the debugger reports that it is executing, below that line you can see the value of each variable):
So…. What’s going on? Is JRE having problems with android? Oh, and no other functions/threads access these variables. Also, it doesn’t happen on my primary phone (android 5.0.1), but it does happen on my secondary phone (4.3).
(I could “expect” a sensor to not exist at all, but that would still violate that if statement).
Edit, full class:
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) {
}
}
And track:
"<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
Solution
(last_grav_reading != null && last_magnet_reading != null)
True after you receive
a gravity event and after you receive a magnet event. There is no code to return last_XXX_reading as null. You mean
if(event.sensor == grav) {
last_magnet_reading = null;
last_grav_reading = event.values;
} else {
last_grav_reading = null;
last_magnet_reading = event.values;
}
Both sensors are connected to the same listener this
sensorManager.registerListener(this, magnet, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, grav, SensorManager.SENSOR_DELAY_NORMAL);