Android Question Java Inline Code in Class or Code Module?

ilan

Expert
Licensed User
Longtime User
hi

i want to use a java inline code from a class module or a code module
but how do i do this?

i put the javaobject in the codemodule and also the java inline code but when i try to run it i get an error
that the java code was not found.

when i run it from the main activity it is working

error: java.lang.RuntimeException: Method: not matched.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
See the inline java tutorial. Especially the sample project. It shows how to use inline java in all module-types
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
oh do you mean that one?
Yes.

See the attached project. It shows how to call Java methods in all types of modules

Check the Sample Project again. There is a Servicemodule, a Classmodule a Codemodule and an Activity module in use
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
i need to make it in a class or module

when i use it in main activity i intialize the java object with

B4X:
nativeMe.InitializeContext

and then i call the nativeMe.runmethod with ...(Array(nativeMe,GetPackageName))

but in class module i get an error Method not matched
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
in the example i see that erel doesnot intialize the javaobject in the class he only say nativeMe = Me

so i do the same but get an error
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Dim a public (JavaObject)nativeMe un Main Process Globals, and try this ;)
B4X:
nativeMe.RunMethod("yourInlineJavaMethod",Array(Main.nativeMe,GetPackageName))
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Code modules are made of static functions so you have to initialize the JavaObject with InitializeStatic not InitializeContext (there's no context; it's not an activity).
B4X:
Dim MyJavaCode As JavaObject
MyJavaCode.InitializeStatic(Application.PackageName & ".mycodemodulename")
MyJavaCode.RunMethod("MyMethod", Null)

#If JAVA
public static void MyMethod() {
...
In a class module, you can do the same.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Thank you @Informatix i will try it out. :)

I have not much experience with the java object but it seems like it is very powerful since you can use 100% java code in your app.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
In Ilan's example, he needs to pass a context to the called Inline Java function. With initializeStatic you can call methods but not use the same JavaObject as a context to be passed as a parameter
I think (may be wrong) we need to pass the context of the activity which initted the class instance, assuming it was "main". (In fact, it would be better if one of the params used to init this class was the same activity's context)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
In Ilan's example, he needs to pass a context to the called Inline Java function. With initializeStatic you can call methods but not use the same JavaObject as a context to be passed as a parameter
I think (may be wrong) we need to pass the context of the activity which initted the class instance, assuming it was "main". (In fact, it would be better if one of the params used to init this class was the same activity's context)

this was exactly my problem yesterday but i passed it by using a global java object from the main activity (like you have suggested)
would interest me to see if i can do it without the global java object.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
In Ilan's example,
Where is it?

he needs to pass a context to the called Inline Java function.
If you need the context in a class module, it's a different story of course.
If you need the application context, you can get it directly in Java with BA.applicationContext. If you need the activity context, you have to pass it.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
what i want to do is to get the result (boolean or string) from the codemodule that uses Content peckagemenager
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
In main, or the activity where you instantiate your class
B4X:
  myClass.initialize(nativeMe)   'this is the global activityContext

and in your class
B4X:
Sub Class_Globals
    Private nativeMe As JavaObject
    Private CallerContext As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize( CallerActivityContext As JavaObject)
    nativeMe = Me
    CallerContext=CallerActivityContext
    Log(nativeMe.RunMethod("mymethod", Array (CallerContext,...)))
End Sub

if it is a code module, then do the same, passing the activity context as one of the arguments of that Sub in the code module
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
In main, or the activity where you instantiate your class
B4X:
  myClass.initialize(nativeMe)   'this is the global activityContext

and in your class
B4X:
Sub Class_Globals
    Private nativeMe As JavaObject
    Private CallerContext As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize( CallerActivityContext As JavaObject)
    nativeMe = Me
    CallerContext=CallerActivityContext
    Log(nativeMe.RunMethod("mymethod", Array (CallerContext,...)))
End Sub

if it is a code module, then do the same, passing the activity context as one of the arguments of that Sub in the code module


This is exactly what i do
But is it possible without the javaobject in main activity?
 
Upvote 0
Top