Android Question Inline Java

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to All
I have some java code that might be useful in my project. I was thinking to insert it with the "#if JAVA ... #end if" paradigm.
As a first step, I created a B4XPages project and just copied the example found in a post here ( besides my situation is more complicate). But even first trial didn't work. So I am missing something very basic, I guess..
The result is that the "FirstMethod is not found". The code is :
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private NativeMe As JavaObject
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")
End Sub


Private Sub Button1_Click
    NativeMe.InitializeContext
    Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
    Log(s)
End Sub

#If JAVA

public String FirstMethod() {
   return "Hello World!";
}

#End if

By the way my needs are to call a function like the following:
B4X:
class xxx
{
    public static List<Integer> YYY(double[] data1, int[] data2 , int aaa)
    { 
       List<Integer> LLL = new ArrayList<>();

       return LLL;
    }
}
 

TILogistic

Expert
Licensed User
Longtime User
other ArrayList (Java)
B4X:
public ArrayList<Object> CalcArrayList (int[] a, float b, double[] c) {
    ArrayList<Object> ResultList = new ArrayList<Object>();
    ResultList.add(a[0] + b / c[0]);
    ResultList.add(a[1] + b / c[1]);
    ResultList.add(a[2] + b / c[2]);
    ResultList.add("text2 Start...");
    ResultList.add("text3 Hello...");
    ResultList.add("text3 Bye...");
    return ResultList;
}
B4X:
    For Each o As Object In JavaInline.CalcArrayList
        LogText(o)
    Next
1691066460973.png
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Yes, finally I got a result and I post here a working project, just to fix the details (if necessary: for me it was). It is still open the question about the structure of the Java code. As a matter of fact I am not thinking to use Java lists instead of B4X ones or use Java for simple operations that can be done with B4X. This is nonsense, forgive me. I am talking of complex code, which is hard to translate in B4X and, moreover, it is "self-contained" in a class. In my initial post, I just wrote an example: my function is included in a class "xxx". Therefore my question was relative to the structure of the Java code, not composed of single functions, callable from B4X. but included in a class. Unluckily nobody observed this fact. Nevertheless, thanks to TILogistic I did a good step ahead. Thanks again.
 

Attachments

  • B4XJavaInlineX.zip
    10.7 KB · Views: 78
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
@GiovanniPolese Look at post #16 - it does what you wanted.
Yes. In theory, my friends. First of all I am grateful for your advices, that of course help a lot. No question about. BUT, we are programmers, If we are able to do something we do it. If not, it means that our question is over our knowledge (my knowledge in this case). In programming, a dot instead of a comma, stopped a Nasa rocket launch. Therefore we should always test our code. I underlined that it is mostly a syntax problem. I know that interfacing is possible. I know Lists. I am not planning to use Java Lists or doing additions and multiplications with Java instead of B4A. The problem is that I don't know the syntax in this case. Going into reality, I did the following, based on your post, NOT knowing anything about Java interfacing, just copying your code and modifying the only thing to modify, that is the b4? to B4A. Result:

"java.lang.ClassNotFoundException: b4A$example$main$MyInlineClass"

and we got back to the beginning ..So, in B4A, I am still missing something ... Thanks anyway again..

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private NativeMe As JavaObject
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")
End Sub


Private Sub Button1_Click
    Dim MyInlineClass As JavaObject
    MyInlineClass.InitializeNewInstance("b4A.example.main$MyInlineClass",Null)  ' change to suit b4j or b4a
    '...
    Log(MyInlineClass.RunMethod("Hello",Array("fred")))
    '...
End Sub


#If Java
public static class MyInlineClass{
       public MyInlineClass(){}

       public String Hello(String s){
                return "Hello there " + s;  
       }  
}
#End if
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If you are unsure of the class you are in, use
B4X:
MyInlineClass.InitializeNewInstance((Me).As(String).Replace("class ","") & "$" & "MyInlineClass",Null)
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
If you are unsure of the class you are in, use
B4X:
MyInlineClass.InitializeNewInstance((Me).As(String).Replace("class ","") & "$" & "MyInlineClass",Null)
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private NativeMe As JavaObject
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")
End Sub


Private Sub Button1_Click
    Dim MyInlineClass As JavaObject
    MyInlineClass.InitializeNewInstance((Me).As(String).Replace("class ","") & "$" & "MyInlineClass",Null)
    'MyInlineClass.InitializeNewInstance("b4a.example.main$MyInlineClass",Null)  ' change to suit b4j or b4a
    '...
    Log(MyInlineClass.RunMethod("Hello",Array("fred")))
    '...
End Sub


#If Java
public static class MyInlineClass{
       public MyInlineClass(){}

       public String Hello(String s){
                return "Hello there " + s;  
       }  
}
#End if

java.lang.ClassNotFoundException: [b4xcollections=null, b4xpages=null, main=null
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I don't use B4XPages normally, I will check it again for you.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
if the java code is in B4XMainPage then use 'b4x.example.b4xmainpage$MyInlineClass'
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If in B4XPages use this ( it should also work in non B4XPages too) (Apologies for not testing previous code with B4XPages)
B4X:
Private Sub Button1_Click
	' get the name of (Me) class	
	Dim thisClass As String = (Me).As(JavaObject).RunMethod("getClass",Null)
	
	'Reference name for the inline java class
	Dim myClass As JavaObject 
	
	'initialise to your inline class (the java code)
	myClass.InitializeNewInstance(thisClass.Replace("class ","") & "$" & "SimpleTest",Null)
	
	' run the method in that class
	Log(myClass.RunMethod("Hello",Array("Freddy")))
End Sub

#if java
public static class SimpleTest{
	public SimpleTest(){}
	
	public String Hello(String s){
		return "Hello " + s;
	}
}
#End If
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If in B4XPages use this ( it should also work in non B4XPages too) (Apologies for not testing previous code with B4XPages)
B4X:
Private Sub Button1_Click
    ' get the name of (Me) class  
    Dim thisClass As String = (Me).As(JavaObject).RunMethod("getClass",Null)
  
    'Reference name for the inline java class
    Dim myClass As JavaObject
  
    'initialise to your inline class (the java code)
    myClass.InitializeNewInstance(thisClass.Replace("class ","") & "$" & "SimpleTest",Null)
  
    ' run the method in that class
    Log(myClass.RunMethod("Hello",Array("Freddy")))
End Sub

#if java
public static class SimpleTest{
    public SimpleTest(){}
  
    public String Hello(String s){
        return "Hello " + s;
    }
}
#End If

I create a sub to make it more readable.
B4X:
Sub JavaInlineClassName (JavaClassName As String) As String
    Dim jo As JavaObject = Me
    Dim s As String = jo.RunMethod("getClass", Null)
    Return s.SubString("class ".Length) & "." & JavaClassName
End Sub

It works in B4A and B4J with different package name.
B4X:
Dim jo As JavaObject
jo.InitializeNewInstance(JavaInlineClassName("SimpleTest"), Null)
Log(jo.RunMethod("Hello", Array("Freddy")))
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Why don't you create a library with what you want?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Using
B4X:
myClass.InitializeNewInstance(GetType(Me) & "$" & "SimpleTest",Null)

Is easier as there is no need for the replace... or substring.
 
Upvote 0
Top