Android Question nativeMe.InitializeContext JAVA code in a class?

welu1805

Active Member
Licensed User
Longtime User
Hi all,

I want to use inline java code like this:

B4X:
Sub Process_Globals
   Dim nativeMe As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     nativeMe.InitializeContext
   End IF
End Sub

' In a button's click methode is this:

Sub btnAdd_Click
  Dim res as int
  res = nativeMe.RunMethod("add", Array As Object(2, 5))
  MsgBox(res, "Result")
End Sub

#IF JAVA

public int add(int a, int b) {
  return a+b;
}

#END IF

This works fine.

But now I want to put this whole code in a classModule like clsAddition and remove the Java code from the Main modul.

B4X:
Sub Class_Globals
   Private nativeMe As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   nativeMe.InitializeContext
End Sub

Sub Addition(val1 as int, val2 as int) as int
   result = nativeMe.RunMethod("add", Array As Object(val1, val2))
End Sub

#IF JAVA

public int add(int a, int b) {
  return a+b;
}

#END IF

If I call "Addition" in the class clsAddition I get an error:

... Method: add not found in: b4a.example.main

That is correct because the Java code is no longer in the Main module but in the class module.

What is to do?

Lutz
 
Top