12

I've implemented the interface GestureDetector.OnGestureListener on my Activity. It was working fine with Android API 32 but after updating the Android API level from 32 to 33, I'm unable to override it's methods. When I roll back to Android API 32 it works fine.

Here is how I'm implementing the interface:

class ABCActivity: AppCompatActivity(), GestureDetector.OnGestureListener {
    
    override fun onDown(p0: MotionEvent?): Boolean = false

    override fun onShowPress(p0: MotionEvent?) = Unit

    override fun onSingleTapUp(p0: MotionEvent?): Boolean = false

    override fun onLongPress(p0: MotionEvent?) = Unit

    override fun onFling(p0: MotionEvent?, p1: MotionEvent?, p2: Float, p3: Float): Boolean = false

    override fun onScroll(
        event: MotionEvent?,
        event1: MotionEvent?,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        // My Logic
    }
}
1
  • please post your actual full code for your onGestureListener. Screenshots are pretty useless
    – user496854
    Commented Aug 24, 2022 at 4:20

3 Answers 3

10

As Vlad said "Removing the ? may cause the app to crash. because the Motion event can still be nullable"

This is a known issue for Google and it is listed on the issue tracker https://issuetracker.google.com/issues/206855618

Even if is e1 defined as not nullable, Android API still produces a null value for it.

After the upgrading compileSDK version to 33 it was not possible to build the project successfully ('onFling' overrides nothing)

One way to solve this issue is to add @Suppress("NOTHING_TO_OVERRIDE", "ACCIDENTAL_OVERRIDE") and setting e1 to nullable

override fun onFling(
            e1: MotionEvent?,
... )

As mentioned before this is a known issue and it should be fixed in the future.

2
  • notice @Suppress("NOTHING_TO_OVERRIDE", "ACCIDENTAL_OVERRIDE") works only when implementing a class like SimpleOnGestureListener, but not for implementing an interface directly, like, OnGestureListener. for that, an @Suppress("ABSTRACT_MEMBER_NOT_IMPLEMENTED") annotation is needed on the class.
    – user160823
    Commented Jul 5, 2023 at 5:25
  • since the compiler static analysis detect no one implements the fling menthod. in the simple detector case, it sees the simple detector implementing
    – user160823
    Commented Jul 5, 2023 at 5:32
8

Ok just figured it out myself that above code was perfectly working with API level 32 and causing problem with API level 33 and the reason was ? operator with MotionEvent. I just removed that and it's working fine for API level 33 as well. The new code looks like this.

class ABCActivity: AppCompatActivity(), GestureDetector.OnGestureListener {

    override fun onDown(p0: MotionEvent): Boolean = false

    override fun onShowPress(p0: MotionEvent) = Unit

    override fun onSingleTapUp(p0: MotionEvent): Boolean = false

    override fun onLongPress(p0: MotionEvent) = Unit

    override fun onFling(p0: MotionEvent, p1: MotionEvent, p2: Float, p3: Float): Boolean = false

    override fun onScroll(
        event: MotionEvent,
        event1: MotionEvent,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        // My Logic
    }
}
2
  • 3
    Removing the ? may cause the app to crash. because the Motion event can still be nullable
    – Vlad
    Commented Nov 14, 2022 at 13:06
  • Yes, we got the crash
    – YouCii
    Commented Jul 13, 2023 at 11:21
6

In my case the function signature changed after updating mindsdkversion 34.

As in decompiled java class function signature is:

boolean onScroll(@Nullable MotionEvent var1, @NonNull MotionEvent var2, float var3, float var4);

so this error occurs

Earlier in kotlin was :

 override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float)

After changing to

override fun onScroll(e1: MotionEvent?, e2: MotionEvent, distanceX: Float, distanceY: Float)

fixes the problem.

Basically 1st argument became Nullable in SDK 34.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.