从android 3.0版开始,Gallery onItemClick和onTouchEvent不能一起工作

我有一个画廊,其中有一个OnItemSelectedListener和一个OnItemClickListener。 所以我可以在图片之间移动,如果我点击一个,其他事情发生(取决于我点击)。 它在V3.0之前运行良好,但是在处理onItemClick事件之后(3.0-4.0),会发生一个致命exception:

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) 

Solutions Collecting From Web of "从android 3.0版开始,Gallery onItemClick和onTouchEvent不能一起工作"

我有同样的问题。 我从onItemClick开始一个活动,这是崩溃的应用程序。

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

我已经实施了“处理程序”,它工作正常。

创build一个处理程序作为一个全局variables:

 private Handler handler; 

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

 handler = new Handler(); 

创build一个post到onItemClick的处理程序,我的看起来像这样:

 handler.post(new Runnable() { @Override public void run() { // Do what ever... } }); 

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