Avoid NullPointerException in the gesture detection code

This commit is contained in:
cketti 2012-05-10 17:19:21 +02:00
parent 7e5717ed81
commit 68a6eddfb6
1 changed files with 21 additions and 0 deletions

View File

@ -135,10 +135,31 @@ public class K9Activity extends Activity {
private static final float SWIPE_MAX_OFF_PATH_DIP = 250f;
private static final float SWIPE_THRESHOLD_VELOCITY_DIP = 325f;
protected MotionEvent mLastOnDownEvent = null;
@Override
public boolean onDown(MotionEvent e) {
mLastOnDownEvent = e;
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Do fling-detection if gestures are force-enabled or we have system-wide gestures enabled.
if (gesturesEnabled || K9.gesturesEnabled()) {
// Apparently sometimes e1 is null
// Found a workaround here: http://stackoverflow.com/questions/4151385/
if (e1 == null) {
e1 = mLastOnDownEvent;
}
// Make sure we avoid NullPointerExceptions
if (e1 == null || e2 == null) {
return false;
}
// Calculate the minimum distance required for this to count as a swipe.
// Convert the constant dips to pixels.
final float mGestureScale = getResources().getDisplayMetrics().density;