0

I need to implement few gestures in activity. I used Genymotion for that, saved gestures file in res/raw folder and wrote a code which is showing all good but keeps crashing the application. Does anyone know what could be the possible reason? I really tried to solve, but it seems i am missing something!

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    GestureOverlayView gesturesView = new GestureOverlayView(this);
    View inflate = getLayoutInflater().inflate(R.layout.activity_garden,
            null);
    gesturesView.addView(inflate);
    gesturesView.addOnGesturePerformedListener(this);
    gestures = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!gestures.load()) {
        finish();
    }

    setContentView(gesturesView);
    }


public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = gestures.recognize(gesture);
    int index = 0;
    double maxScore = predictions.get(index).score;
    for (int i = 1; i < predictions.size(); i++) {
        if (predictions.get(i).score > maxScore) {
            index = i;
            maxScore = predictions.get(i).score;
        }
    }
    Prediction p = predictions.get(index);

    if (p.name.equalsIgnoreCase("Love"))
        daisy.setImageResource(sp.getInt("Love", 0));
    if (p.name.equalsIgnoreCase("Hit"))
        daisy.setImageResource(sp.getInt("Hit", 0));
    if (p.name.equalsIgnoreCase("Pet"))
        daisy.setImageResource(sp.getInt("Pet", 0));
    Toast.makeText(this, p.name + "\n" + p.score, Toast.LENGTH_SHORT)
            .show();
}

1 Answers1

0

On your code, what happen if you don't get any prediction?

Well, index will be equals to 0, and predictions.get(index) will crash because there is no object at index 0.

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = gestures.recognize(gesture);
    if (predictions == null || predictions.isEmpty()) {
        return;
    }
    // continue the regular flow
}
Simon Marquis
  • 7,248
  • 1
  • 28
  • 43