从Android 3.0版开始,Gallery onItemClick和onTouchEvent无法一起使用

时间:2011-12-07 19:05:33

标签: android nullpointerexception gallery android-gallery onitemclicklistener

我有一个Gallery,它有一个OnItemSelectedListener和一个OnItemClickListener。所以我可以在图片之间移动,如果我点击一个,会发生其他事情(取决于我点击了哪些)。 它在版本3.0之前工作正常,但在处理onItemClick事件后从那里(3.0-4.0),会发生致命异常:

12-07 18:46:03.008: E/AndroidRuntime(935): FATAL EXCEPTION: main
12-07 18:46:03.008: E/AndroidRuntime(935): java.lang.NullPointerException
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.GestureDetector.onTouchEvent(GestureDetector.java:587)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.widget.Gallery.onTouchEvent(Gallery.java:937)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.View.dispatchTouchEvent(View.java:5462)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1947)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1708)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1722)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1722)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1722)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1722)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1722)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1722)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1722)
12-07 18:46:03.008: E/AndroidRuntime(935):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1882)
12-07 18:46:03.008: E/AndroidRuntime(935):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1361)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
12-07 18:46:03.008: E/AndroidRuntime(935):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1830)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.View.dispatchPointerEvent(View.java:5638)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2827)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2406)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.os.Looper.loop(Looper.java:137)
12-07 18:46:03.008: E/AndroidRuntime(935):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-07 18:46:03.008: E/AndroidRuntime(935):  at java.lang.reflect.Method.invokeNative(Native Method)
12-07 18:46:03.008: E/AndroidRuntime(935):  at java.lang.reflect.Method.invoke(Method.java:511)
12-07 18:46:03.008: E/AndroidRuntime(935):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-07 18:46:03.008: E/AndroidRuntime(935):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-07 18:46:03.008: E/AndroidRuntime(935):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:10)

我遇到了同样的问题。我正在从onItemClick开始一个Activity,那就是崩溃了应用程序。

问题,我认为是尝试做一些应该只在主线程上做的事情(比如开始一个活动)。

我已经实现了“处理程序”,它运行正常。

将Handler创建为全局变量:

private Handler handler;

将处理程序设置在onCreate中,使其位于主线程上:

handler = new Handler();

在onItemClick中为处理程序创建一个帖子,我看起来像这样:

handler.post(new Runnable() {

    @Override
    public void run() {
        // Do what ever...
    }
});

答案 1 :(得分:0)

我在onTouchEvent()获得了NPE:587。所以我开始使用支持库中的GestureDetectorCompat,它工作正常。