Android Question Inline Callback : Constructor not found

jkhazraji

Active Member
Licensed User
Longtime User
Concerning this post: https://www.b4x.com/android/forum/threads/implementing-a-callback-with-inline-java.168878/
When I run the following code, an error is issued: Constructor not found at line:
B4X:
    callback.InitializeNewInstance(GetType(Me) & "$MyNetworkCallback", Array(Me)) '$ + class name. And pass Me to the constructor
The code (in B4Xpages) is:
B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=%PROJECT_NAME%.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
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")
    Dim callback As JavaObject
    Dim r As Reflector
    callback.InitializeNewInstance(GetType(Me) & "$MyNetworkCallback", Array(Me)) '$ + class name. And pass Me to the constructor
    Wait For Network_State (Available As Boolean, Network As Object)
    Log($"${Available}: ${Network}"$)
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

#if Java
public static class MyNetworkCallback extends android.net.ConnectivityManager.NetworkCallback {
    private final BA ba;
    public MyNetworkCallback(BA me) {
        this.ba = me;
    }
    @Override
        public void onAvailable(android.net.Network network) {
            super.onAvailable(network);
            ba.raiseEvent(this, "network_state", true, network);
        }
        @Override
        public void onUnavailable() {
            super.onUnavailable();
            ba.raiseEvent(this, "network_state", false, null);
        }
}
#End If
I tried to replace: Array(Me) using reflection library as:
B4X:
Dim r as Reflector
callback.InitializeNewInstance(GetType(Me) & "$MyNetworkCallback", Array(r.GetActivityBA)) '$ + class name. And pass Me to the constructor
But nothing happens: Wait For .. is not implemented
What is the correct code please. @Erel
 

jkhazraji

Active Member
Licensed User
Longtime User
The constructor parameter type is wrong.

It should be:
B4X:
 private final BA ba;
    public MyNetworkCallback(B4AClass me) {
        this.ba = me.getBA();
    }
It runs but the network_state sub is not evoked.
N.B. I am using an emulator
 
Upvote 0
Top