1
public class TestActivity extends Activity implements OnGesturePerformedListener {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
    gestures.addOnGesturePerformedListener(TestActivity.this);

}

我的 main.xml

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/ic_launcher" />

<android.gesture.GestureOverlayView
    android:id="@+id/gestureOverlayView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</android.gesture.GestureOverlayView>

活动.java:

private GestureLibrary mLibrary;

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
    Log.v("performed", "performed");
    // We want at least one prediction
    if (predictions.size() > 0) {
        Prediction prediction = predictions.get(0);
        // We want at least some confidence in the result
        if (prediction.score > 1.0) {
            if (prediction.name.equalsIgnoreCase("right swipe")) {
                // do you thing here//
                Toast.makeText(TestActivity.this, "rigth",
                        Toast.LENGTH_SHORT);
            } else if (prediction.name.equalsIgnoreCase("left swipe")) {

                Toast.makeText(TestActivity.this, "lef",
                        Toast.LENGTH_SHORT);
            }
        }
    }
}

运行此程序会在 Log 中显示以下内容:

05-17 15:18:52.124: E/AndroidRuntime(17266): FATAL EXCEPTION: main
05-17 15:18:52.124: E/AndroidRuntime(17266): java.lang.RuntimeException: Unable to start activity ComponentInfo{t.e.st/t.e.st.TestActivity}: java.lang.NullPointerException
05-17 15:18:52.124: E/AndroidRuntime(17266):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at android.os.Looper.loop(Looper.java:130)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at android.app.ActivityThread.main(ActivityThread.java:3835)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at java.lang.reflect.Method.invokeNative(Native Method)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at java.lang.reflect.Method.invoke(Method.java:507)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at dalvik.system.NativeStart.main(Native Method)
05-17 15:18:52.124: E/AndroidRuntime(17266): Caused by: java.lang.NullPointerException
**05-17 15:18:52.124: E/AndroidRuntime(17266):  at t.e.st.TestActivity.onCreate(TestActivity.java:26)**
05-17 15:18:52.124: E/AndroidRuntime(17266):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-17 15:18:52.124: E/AndroidRuntime(17266):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
05-17 15:18:52.124: E/AndroidRuntime(17266):    ... 11 more

请帮我解决这个问题。

覆盖在 xml 中。所以这不是问题,它说错误在第 26 行。所以它的 abt addOnGesturePerformedListener();

4

1 回答 1

3

如果你确定你的布局中有一个with ,那么View试试@+id = "gestureOverlayView1"main.xml

gestures.addOnGesturePerformedListener(this); 

代替

gestures.addOnGesturePerformedListener(TestActivity.this);

(从 中删除说明TestActivitythis

于 2012-05-17T10:11:47.843 回答