Android Question Method: Initialize not matched" error with JavaObject.RunMethod (Minimal Example)

seyed_27

Member
Hi everyone,

I'm encountering a persistent java.lang.RuntimeException: Method: Initialize not matched error, even in a minimal project, when trying to call an inline Java method from B4A using JavaObject.RunMethod. I'm hoping someone can spot what I might be missing.

Environment:

  • B4A Version: 13.10
  • Java Version (JDK): 19
  • Target Android version (if relevant, though error is at compile/runtime linkage): Android 7 (from logs)
Problem Description: I'm trying to call an instance method in my inline Java code from the Activity_Create sub of my Main module. The B4A code seems to correctly prepare the JavaObject and call RunMethod with arguments that match the Java method's signature, but I consistently get the "Method not matched" error.

Minimal B4A Code (Main.bas):
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private WebView1 As WebView
End Sub

 


Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout") 
    Dim Native As JavaObject = Me
    Log("B4A: Calling Initialize with BA and WebView")
    
 
 
    Native.RunMethod("Initialize", Array(Me, WebView1)) ' Me = reference to the activity BA object

    
    Log("B4A: Call to InitializeJava finished.")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

 

#if JAVA
    import anywheresoftware.b4a.BA;
    import android.webkit.WebView;

    private static BA parent;
    private static WebView webView;
  
    public void Initialize(BA ba, WebView wv) {
        parent = ba;
        webView = wv;
        BA.Log("✅ Java Initialize called. BA = " + parent);
    }
    
    

#End If

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
B4A: Calling Initialize with BA and WebView
Error occurred on line: 36 (Main) 'Line where Native.RunMethod is called
java.lang.RuntimeException: Method: Initialize not matched.
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at b4a.web.main.afterFirstLayout(main.java:107) ' My package name is b4a.web
    at b4a.web.main.access$000(main.java:19)
    at b4a.web.main$WaitForLayout.run(main.java:85)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6161)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
** Activity (main) Resume **

What I've Checked/Tried:

  1. Signature Matching: The B4A call Native.RunMethod("Initialize", Array(Me, WebView1)) seems to perfectly match the Java instance method public void Initialize(BA ba, WebView wv).
    • Me in B4A Activity is a BA instance.
    • WebView1 is an android.webkit.WebView instance.
  2. Case Sensitivity: Checked that "Initialize" matches in both B4A and Java.
  3. Clean Project: I have tried cleaning the project and recompiling.
  4. Minimal Project: The code above is from a brand new, minimal project created just to test this, and the error persists.
  5. Static vs. Instance: I have also tried defining the Java method as static and calling it appropriately, but the "Method not matched" error remained (though the previous ClassCastException or cannot find symbol ba errors were when trying different Java structures). The current setup (B4A Native=Me calling a non-static Java method) seems like it should be the most straightforward.
I'm at a loss as to why B4A cannot match this method. The types and argument count appear correct. Is there anything obvious I'm overlooking, or could this be related to my environment (B4A 13.10 with JDK 19)?

Any insights or suggestions would be greatly appreciated!

Thanks,
 

Attachments

  • 122.rar
    3.1 KB · Views: 61

stevel05

Expert
Licensed User
Longtime User
I'm not sure what you are trying to do.

The BA object is already available in the java code.
Keyword 'Me' references the Class (Main Code Module in this case is b4a.web.main).
In a code module, your java method needs to be static.
 
Last edited:
Upvote 0

seyed_27

Member
I'm not sure what you are trying to do. The BA object is already available in the java code. Keyword 'Me' references the Class (Main Code Module in this case is b4a.web.main).

In a code module, your java method needs to be static.
I'm currently working on converting a project that was originally structured using B4XPages to a standard B4A Main Activity structure. I'm encountering a persistent java.lang.RuntimeException: Method: Initialize not matched error when trying to call an inline Java method from my Main module, even with a minimal setup.

I understand from previous forum advice that Java methods called via JavaObject.RunMethod from a B4A code module (like an Activity module) should generally be declared as static. I've tried to implement this, but the issue persists.

Environment:

  • B4A Version: 13.10
  • Java Version (JDK): 19
  • The error occurs when running on an emulator/device (Android 7 reported in logs).
Problem Context: The original B4XPages project had inline Java code that worked within its page classes. Now, in the Main Activity module, I'm trying to achieve similar Java interop.
 

Attachments

  • B4A.rar
    20.8 KB · Views: 70
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The initial problem is on Line 375. You can't use 'this' in a static class. Because of the raiseEvent's you need to use I think you will need to run this code in a standard Class. Or probably better, create a Customview and include the Webview in that.
 
Last edited:
Upvote 0

seyed_27

Member
The initial problem is on Line 375. You can't use 'this' in a static class. Because of the raiseEvent's you need to use I think you will need to run this code in a standard Class. Or probably better, create a Customview and include the Webview in that.
B4X:
Error occurred on line: 26 (Main)
java.lang.RuntimeException: Method: Initialize not matched.
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
    at b4a.web.main._activity_create(main.java:402)
    ...


I want to pass the current Activity context (Me) and the WebView instance to a native Java method called Initialize.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout") 
    Dim Native As JavaObject = Me
    Native.RunMethod("Initialize", Array(Me, WebView1)) 
End Sub


#if JAVA
import anywheresoftware.b4a.BA;
import android.webkit.WebView;

private static BA parentActivityBA; 
private static WebView mainWebView;   

public static void Initialize(final BA baContext, final WebView wv_param) {
    parentActivityBA = baContext; 
    mainWebView = wv_param;   

    if (parentActivityBA != null) {
        parentActivityBA.Log("✅ Java STATIC Initialize (called via InitializeStatic): BA context received: " + parentActivityBA.toString());
    } else {
        BA.Log("❌ Java STATIC Initialize (called via InitializeStatic): Received baContext is null!");
    }
    if (mainWebView != null) {
        if (parentActivityBA != null) parentActivityBA.Log("✅ Java STATIC Initialize: WebView received: " + mainWebView.toString()); 
        else BA.Log("✅ Java STATIC Initialize: WebView received: " + mainWebView.toString() + " (parent BA was null for logging)");
    } else {
         if (parentActivityBA != null) parentActivityBA.Log("❌ Java STATIC Initialize: Received wv_param (WebView) is null!");
         else BA.Log("❌ Java STATIC Initialize: Received wv_param (WebView) is null! (parent BA was null for logging)");
    }
}
#endif



I tried passing both Me and WebView1 from B4A to Java using:
B4X:
Native.RunMethod("Initialize", Array(Me, WebView1))

In Java, I declared the method as:

B4X:
public static void Initialize(final BA baContext, final WebView wv_param)

But this results in the error:



B4X:
java.lang.RuntimeException: Method: Initialize not matched.

I've attached a minimal version of my project , stripped of all unnecessary code, so you can reproduce the issue easily.


🔽 Attached File: Minimal_WebView_Native_Example.zip
This zip file contains only:

  • A single Activity
  • A WebView
  • The relevant Native Java code
  • Assets needed to run the example


❓ Question:

How can I correctly declare and call a native Java method from B4A that accepts both the Activity context (Me) and a WebView object?


Is there any limitation or special syntax required when passing complex types like BA or WebView through RunMethod?



Additional Info:

  • B4A Version: 13.10
  • Target SDK: Android 10+
  • Testing on emulator and real device

Any help would be greatly appreciated!



✅ Update After Solution (Optional Later):

Once we find a solution, you can add it here so others can benefit too.



🙏 Thank you in advance for your help!​

 

Attachments

  • 122.rar
    3.2 KB · Views: 56
Upvote 0

stevel05

Expert
Licensed User
Longtime User
See post #2
Keyword 'Me' references the Class (Main Code Module in this case is b4a.web.main).
and post #4
Because of the raiseEvent's you need to use I think you will need to run this code in a standard Class. Or probably better, create a Customview and include the Webview in that.
If you get this working, it won't be the last of the issues you face.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
not sure maybe (Android not my strong suit)
B4X:
parentActivityBA = baContext.getBA();
 
Upvote 0

seyed_27

Member
not sure maybe (Android not my strong suit)
B4X:
parentActivityBA = baContext.getBA();
B4X:
B4A Version: 13.10
Parsing code.    (0.00s)
    Java Version: 19
Building folders structure.    (0.02s)
Compiling code.    (0.03s)
Compiling layouts code.    (0.03s)
Organizing libraries.    (0.06s)
    (AndroidX SDK)
Compiling resources    (0.10s)
Linking resources    (0.19s)
    build tools: 34.0.0, android jar: android-34
Compiling debugger engine code.    (2.93s)
Compiling generated Java code.    Error
src\b4a\web\main.java:419: error: cannot find symbol
        parentActivityBA = baContext.getBA();
                                    ^
  symbol:   method getBA()
  location: variable baContext of type BA
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

javac 19.0.2
 
Upvote 0
Top