我有一个图库,其中包含一个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)

最佳答案

我有同样的问题。我从onItemClick开始一个Activity,这使该应用程序崩溃了。

我认为问题在于尝试执行仅应在主线程上执行的操作(例如启动 Activity )。

我已经实现了一个“处理程序”,并且工作正常。

创建一个Handler作为全局变量:

private Handler handler;

将处理程序设置在onCreate内,使其位于主线程上:
handler = new Handler();

在onItemClick中为处理程序创建一个帖子,我的看起来像这样:
handler.post(new Runnable() {

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

关于android - 自Android 3.0版起,图库onItemClick和onTouchEvent无法一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8420988/

10-09 03:37