Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gallery onItemClick and onTouchEvent don't work together since android version 3.0

I have a Gallery, which has an OnItemSelectedListener and an OnItemClickListener. So I can move between the pictures, and if i click on one, something else happens (depends on which i had clicked). It worked fine before version 3.0, but from there (3.0-4.0) after it handles the onItemClick event, a fatal exception occurs:

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)
like image 838
abbath Avatar asked Dec 07 '11 19:12

abbath


1 Answers

I had the same problem. I was starting an Activity from onItemClick and that was crashing the app.

The problem, I think is with trying to do something that should be done only on the main thread (like starting a activity).

I have implemented a "Handler" and it works fine.

Create a Handler as a global variable:

private Handler handler;

Set the handler inside onCreate so it is on the main thread :

handler = new Handler();

Create a post to the handler in onItemClick, mine looks like this:

handler.post(new Runnable() {

    @Override
    public void run() {
        // Do what ever...
    }
});
like image 86
Valer Dragos Avatar answered Sep 19 '22 03:09

Valer Dragos