B4J Question Callsub problem with parameters

luki_c

Member
I would like to call the return function from a separate class using callSub2 Test code:

class test:
Sub Class_Globals
    Private fx As JFX
    
    Private callback As Object
    Private timer1 As Timer
    Private functname As String
    Private x As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(mCallback As Object,funct As String , interval As Int)
    
    timer1.Initialize("timer1",interval)
    timer1.Enabled = True
 callback = mCallback
    functname = funct
   End Sub

private Sub timer1_Tick
    
Log("Timer tick")
    x = x + 1
    CallSub2(callback, functname,x)
End Sub

B4xMainPage:

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private test1 As test
    Private obj As Object
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True

    

End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    test1.Initialize(Me,callbackFnc(obj),1000)
    
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
 Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    
    Log("test")
    
End Sub


Sub callbackFnc( test11 As  Object)
    
    'Log(test11)
    
End Sub
Returns an error:

B4X:
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Timer tik
Error occurred on line: 33 (test)
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.Exception: Sub  was not found.
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:522)
    at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:468)
    at b4j.example.test._timer1_tick(test.java:106)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.objects.Timer$TickTack$1.run(Timer.java:135)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)

How can I fix it? Sorry if there was already a similar thread, I need a return function with parameters
 

luki_c

Member
Ok I guess I wrote the post too quickly, typing the function name as a string fixed the problem. Though interesting that when the return function didn't take parameters I didn't have to do that :)
B4X:
    test1.Initialize(Me,"callbackFnc",1000)
 
Upvote 0
Top