gpt4 book ai didi

android - Gallery onItemClick 和 onTouchEvent 自 android 版本 3.0 起无法协同工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:11 26 4
gpt4 key购买 nike

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

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 - Gallery onItemClick 和 onTouchEvent 自 android 版本 3.0 起无法协同工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8420988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com