Android Question Variable Reference to a view not stable ?

MitchBu

Well-Known Member
Licensed User
Longtime User
I need to get the view at a point on the screen for later processing, so I use an overlaid panel all over the activity to get x and y.

Then I have a sub that sets a global variable created in sub Globals
ViewToMove as View
to the control at these coordinates :

B4X:
Sub ViewAtXY(x, y) As Int

            For mCnDx = 0 To PnlCheck.NumberOfViews-1

                If x >= PnlCheck.GetView(mCnDx).Left And x <= PnlCheck.GetView(mCnDx).Left + PnlCheck.GetView(mCnDx).Width And _
                y >= PnlCheck.GetView(mCnDx).Top And y <= PnlCheck.GetView(mCnDx).Top + PnlCheck.GetView(mCnDx).Height And _
                PnlCheck.GetView(mCnDx) <> PnlGreenBackground Then
                    Log("found")
                    ViewToMove = PnlCheck.GetView(mCnDx)
                    Log("vtm " & ViewToMove.Tag)
                    Return mCnDx
                End If
            Next

            Return -1 'Not found
end sub

I am puzzled because sometimes it works as expected, but I also get errors view not initialized (below).

It looks as if somehow ViewToMove no longer points to the view it was assigned in ViewAtXY.

What am I missing ?

main_pnltouch_touch (B4A line: 1585)
Log("Drag " & ViewToMove.Tag)
java.lang.RuntimeException: Object should first be initialized (View).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.ViewWrapper.getTag(ViewWrapper.java:232)
at b4a.CheckPrintR.main._pnltouch_touch(main.java:4226)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA$1.run(BA.java:293)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6134)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

PS : I looked through the forum, in particular https://www.b4x.com/android/forum/threads/variables-objects-in-basic4android.8385/

and it looks as the variable should keep its value, since I am not changing it.
 
Last edited:

MitchBu

Well-Known Member
Licensed User
Longtime User

Thanks, but it does not change the fact the the global variable ViewToMove seems to become local to the method, and loses validity outside of that sub. Yet I am not Dimming it there, so I believe it should remain global.

I can get the tag from it inside the sub, but not in PnlTouch touch event. That is my big problem.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Try declare this in Starter.
Dim ViewToMove As View

and recall this as:
B4X:
...
Log("found")
Starter.ViewToMove = PnlCheck.GetView(mCnDx)
Log("vtm " & ViewToMove.Tag)
....
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…