Android:addOnGesturePerformedListener 中的空指针异常

标签 android nullpointerexception gesture

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>

Activity .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);
            }
        }
    }
}

运行此崩溃,日志中显示以下内容:

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

请帮我解决这个问题。

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

最佳答案

如果您确定在您的 main.xml 布局中有一个带有 @+id = "gestureOverlayView1"View,请尝试

gestures.addOnGesturePerformedListener(this); 

代替

gestures.addOnGesturePerformedListener(TestActivity.this);

(从 this 中删除 TestActivity 说明符)

关于Android:addOnGesturePerformedListener 中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10633502/

相关文章:

java - 配置 Hibernate Validator 不使用 hashCode()

c# - 在 Unity 中使用 GestureRecogniser 双击全息镜头

Flutter - 检测屏幕上充满其他小部件的点击

Flutter:如何移动、旋转和缩放容器?

android - AWS Mobile Hub 示例项目中 PushManager 类的空对象引用

android - 为什么我使用 StickyGridHeaders 库得到 NullPointerException,而其他人使用同一个库就好了?

Android:为什么取android app的名字作为启动activity的名字?

java - android-support-v7-appcompat 库项目将无法运行

android - 刷新 ListView 中的复选框状态

java - 简单框架 XML 反序列化