Android Question Wait For inside Loop ??

ilan

Expert
Licensed User
Longtime User
hi

i am trying to use a wait for inside a loop but i get an error.
i am doing it inside a Do...While loop. using sleep() does work but using a wait for() not.
am i doing something wrong or its not possible to do that like that?

relevant code:

B4X:
'...
Wait For(drawsmoohtplayer) Complete (finished As Boolean)
    If finished Then
        If keepgoing Then
            ismoving = True
             trykeepgoing(direction)                     
       End If
   End If
'...


Sub drawsmoohtplayer As ResumableSub
    Dim lastcell As cell = grid.Get(lastcellpos)
    Dim cell As cell = grid.Get(myPlayer.pPos)
    Dim difx As Float = (cell.left - lastcell.left) / 6
    Dim dify As Float = (cell.top - lastcell.top) / 6
    For i = 1 To 6 '6 frames
        cONMAZE.ClearRect(cONMAZE.TargetRect)
        Dim r As B4XRect
        r.Initialize(lastcell.left+(w*0.22)+xCenter+(difx*i),lastcell.top+(w*0.22)+(dify*i),lastcell.right-(w*0.22)+xCenter+(difx*i),lastcell.bottom-(w*0.22)+(dify*i))
        cONMAZE.DrawRect(r,xui.Color_White,True,0)
        cONMAZE.Invalidate
        Sleep(20)
    Next
    Return True
End Sub

error:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
44 ms
levelmapsize: 100
1 ms
Error occurred on line: 319 (Main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:479)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:293)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$1.run(BA.java:335)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.NoSuchMethodError: No virtual method _move(Lwww/sagital/maze/madmaze;IZ)Ljava/lang/String; in class Lwww/sagital/maze/madmaze; or its super classes (declaration of 'www.sagital.maze.madmaze' appears in base.apk)
at www.sagital.maze.main._gamepnlin_touch(main.java:1370)
... 14 more
Error occurred on line: 319 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[])' on a null object reference
at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:479)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:293)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$1.run(BA.java:335)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Error occurred on line: 319 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[])' on a null object reference
at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:479)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:293)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$1.run(BA.java:335)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
** Activity (main) Pause, UserClosed = true **
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
So, I think your Sleep(20) inside the loop maybe is returning "finished" as null.
It doesn't return null because he correctly calls drawsmoothplayer with Wait For.

This code should work.
Similar example:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log("before")
   Wait For (Test) Complete (Finish As Boolean)
   Log("after")
End Sub

Sub Test As ResumableSub
   For i = 1 To 6
       Log(i)
       Sleep(500)
   Next
   Return True
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
It doesn't return null because he correctly calls drawsmoothplayer with Wait For.

This code should work.
Similar example:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log("before")
   Wait For (Test) Complete (Finish As Boolean)
   Log("after")
End Sub

Sub Test As ResumableSub
   For i = 1 To 6
       Log(i)
       Sleep(500)
   Next
   Return True
End Sub

I dont see any difference between your code and mine erel. Am i missing something?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What is calling the code that has the wait for?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
No. I expect your code to work. The problem is somewhere else...

i have changed the structure of the code and it is working now but what i did is i have put the wait for in a do while loop and i got those errors. now i dont do it like this
i create a list and add all vectors to the list in the do while loop and then i call the animation and this is working well.

the weired thing is that i tried to reproduce the error i got yesterday in b4j but it is working. could it be that it is a bug in b4a? i dont have b4a at work so i cannot test it now. this is the code that should raise those errors

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
   
    Dim nr As Int = 0
   
    Do While nr < 5
        nr = nr + 1
        Log("before wait for")
        Wait For (test) Complete (fin As Boolean)
        Log("after wait for")
    Loop
   
    Log("end of test")
   
End Sub

Sub test As ResumableSub
    Log("test was called")
    Sleep(50)
    Return True
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
was i am missing in
drawsmoohtplayer is
Dim i as int
and
w as sub input parameter.
grid and cONMAZE can also be a functional parameter
 
Upvote 0
Top