Android Question [SOLVED] Setting up an on touch listener with the reflection library

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

Can you check my coding? I'm trying to set up an on touch listener on one of my image views with the reflection library. I can compile the app without errors but when I run the app the logs indicate this error:

B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference

I think it's because I'm missing some additional code. Can you tell me what additional coding I need to add or correct? Which object is null and how to I change the coding so it's not null?

Thanks.

B4X:
Sub InitializeObjects

    theReflector.SetOnTouchListener("MapTouch")
    theReflector.Target = MoldMapView
    
    strShared = Starter.rp.GetSafeDirDefaultExternal("Settings")
    
    ' This is for the KeyValueStore database. It stores a Key and a value.
    '---------------------------------------------------------------------
    kvs.Initialize(strShared, "Settings")
End Sub

Sub MapTouch_Touch(ViewTag As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean

    ToastMessageShow("Tapped location x = " & X & " and y = " & Y, True)

    Return True
End Sub
 

rleiman

Well-Known Member
Licensed User
Longtime User
You should set the target first.
B4X:
theReflector.Target = MoldMapView
theReflector.SetOnTouchListener("MapTouch")
Hi LucaMS,

Thanks. I never would have guessed about changing the order of those 2. :)

I also moved the call to the Initialize sub routine after loading the layout. Looks like MoldMapView is not null when the layout is loaded.
 
Upvote 0
Top