活动中的手势 - 安卓

时间:2014-08-26 06:53:53

标签: java android

我需要在活动中实现一些手势。我使用了Genymotion,在res / raw文件夹中保存了手势文件,并编写了一个显示所有优点但仍然使应用程序崩溃的代码。有谁知道可能的原因是什么?我真的试图解决,但似乎我错过了一些东西!

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 个答案:

答案 0 :(得分:0)

在您的代码中,如果您没有得到任何预测会发生什么?

好吧,index将等于0predictions.get(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
}