Android Question Inline Java classes?

JordiCP

Well-Known Member
Licensed User
I don't know if it has been posted somewhere, but at least I haven't been able to find it searching in the forum

Can someone post a simple example (or link) of how to declare a simple class with inline Java and then use it from the B4A code? for instance a class with just one variable and one function.

I have been trying with examples similar to THIS , which does not work, trying to change things, but with no succes.:(
 

JordiCP

Well-Known Member
Licensed User
Well, the main purpose is learning, so it is a kind of exercise for myself. Also, I must recognize that I have done things with Java but still don't feel totally comfortable with it. :confused:

Let's suppose I want to declare, fully in Java, an object "dot_in_a_line" with some members (variables and functions)

int xpos; // not visible from the outside
int step; // not visible
init( x_ini,step_ini ) --> sets xpos=x_ini, step=step_ini
goright() --> sets xpos+=step
goleft() --> sets xpos-=step
int getpos() --> returns xpos

And then want to declare N instances of this object from b4a and use them.


Usually I declare a class in B4A to achieve this and then I can instantiate as many objects as I want.
 

thedesolatesoul

Expert
Licensed User
Lets say for e.g. you use inline java to create a class within an activity. That class can only be instantiated within the activity. It is in the parent class scope, it is not visible from anywhere else.

Admittedly, I havent really tried to do it, and I am not seeing a strong use for it. Maybe someone else can shed some light on it.
 

JordiCP

Well-Known Member
Licensed User
Yes, the purpose is to use it from the same activity. And I agree that it can be perfectly accomplished by other means if it is needed.

Anyway, my question is more related to the "is it possible to make it work correctly?" because I am getting errors


For instance, if I declare a class "myclass" from Main and my packagename is "jcp.b4a.myexample", the only way I can get access to the class is

B4X:
Dim OO as JavaObject
OO.InitializeNewInstance("jcp.b4a.myexample.main.myclass",Null)
But then I face other runtime errors such as "no empty constructor found", even if I provide one....

So I think Erel has the key to it.
 

JordiCP

Well-Known Member
Licensed User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private NativeMe As JavaObject
    Dim OO As JavaObject
    Dim Timer1 As Timer
End Sub

Sub Globals

End Sub

Sub Activity_Create(firsttime As Boolean)
   
    NativeMe.InitializeContext
    OO.InitializeContext

    'OO=NativeMe.InitializeStatic("jcp.b4a.thirdlib.main.mylib")
       
    'Error --> no empty constructor en runtime
    OO=NativeMe.InitializeNewInstance("jcp.b4a.thirdlib.main.mylib",Null)
    Dim qqo As JavaObject
    Dim qq As Int=2
    qqo=qq

'This gives--> no constructor found
    'OO=NativeMe.InitializeNewInstance("jcp.b4a.thirdlib.main.mylib",Array(qqo))

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub


#If java

class mylib{

    public mylib(){}

    //public mylib( int k){ pos=k;}
    int pos;
   
    public void go_right(int step){
        pos+=step;
    }
    public void go_left(int step){
        pos-=step;
    }

}

#End If
 

JordiCP

Well-Known Member
Licensed User
Thanks Erel, now it is running ok

I understand what you say, but it helps me a lot to learn how it all works :)
 
Top