Android Question Inline java in Custom view module not working

Jerryk

Member
Licensed User
Longtime User
I have code module:

B4X:
Sub Process_Globals
    Private nativeMe As JavaObject
End Sub

Sub Locale(item As String) As String
    nativeMe.InitializeContext
    Dim id As Int = nativeMe.RunMethod("getResourceId", Array("string", item))
    If id > 0 Then
        Return nativeMe.RunMethod("GetString", Array(id))
    Else
        Return item
    End If
End Sub

Sub Plural(item As String, count As Int) As String
    nativeMe.InitializeContext
    Dim id As Int = nativeMe.RunMethod("getResourceId", Array("plurals", item))
    If id > 0 Then
        Return nativeMe.RunMethod("FormatPlural", Array(id, count))
    Else
        Return item
    End If
End Sub

#if Java
    public int getResourceId(String type, String name) {
            return BA.applicationContext.getResources().getIdentifier(name, type, BA.packageName);
        }
    public String FormatPlural(int id, int count) {
            return BA.applicationContext.getResources().getQuantityString(id, count, count);
        }
    public String GetString(int id) {
            return BA.applicationContext.getResources().getString(id);
        }
#End If

When I call the Plural or Locale methods from the Activity everything is ok. But when I call these methods in the Custom view module, an error occurs
java.lang.RuntimeException: Method: getResourceId not found in: gpb.example.apokus. Apokus is the Activity where I display my Custom view module. Any solution?
 
Last edited:

mangojack

Expert
Licensed User
Longtime User
Certainly not an expert in this area, but possibly it should be ..
B4X:
Sub Locale(item As String) As String
    nativeMe = Me

See example Here * regarding using Inline Jave in a Class or Code Module.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I try:
Code modules do not support Me keyword.

Did you even download and look at the example link I posted above ?

The Example project by @Erel shows correct code for inline Java for both Class and Code Module.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I modified the Erel project indicated by @mangojack, adding a custom view and the java code works.

(It is not a CV B4X, the project is old and strangely it is not possible to create a CV B4X class, the IDE does not provide it. However it works and will surely work also in a B4XPages project with CV B4X).
 

Attachments

  • InlineJava_In_CV.zip
    9.9 KB · Views: 35
Upvote 0

teddybear

Well-Known Member
Licensed User
Attaching a test project, I took a ClockView and added a call to Class1. It falls on:
Dim id As Int = nativeMe.RunMethod("getResourceId", Array("plurals", item))
The modified project is attached
 

Attachments

  • CustVmod.zip
    11.1 KB · Views: 34
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…