Android Question how to use Java Object

gezueb

Active Member
Licensed User
Longtime User
I have to write an App that should communicate with a Navigation Tool which is written in Java. There's an API library called navappclient.jar which I included in the #additionaljar. i cannot figure out how to access the methods in this library. I know there is a tutorial "accessing third party Java" with an example, but frankly i am too dumb to translate this to the given API.
The API says the following:
/quote
To use the NavApp SDK you need to create an instance of the NavAppClient, using the NavAppClient.Factory.make(Context, ErrorCallback) method. The returned instance is used to access the individual APIs.

The package is called com.tomtom.navapp.

The interface (i shoul use) is called Routable
public interface Routeable
extends com.tomtom.navapp.internals.Data
This interface is holding the data relating to any geographic location on the map. A Routeable can contain a latitude/longitude, and an address associated with this point.
end quote/

And to begin with, i would like to call the methods double getLatitude() and double getLongitude()

Any help how to access these methods in b4a would be great, thank you.
Georg

P.S. I have b4a 3.82
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
com.tomtom.navapp.NavAppClient.Factory.make is a static method.

B4X:
Dim Factory As JavaObject
Dim client As JavaObject = Factory.InitializeStatic("com.tomtom.navapp.NavAppClient.Factory").RunMethod("make", Array(GetContext, null))
Dim lat = 33, lon = 33 As Double
Dim r As JavaObject = client.RunMethod("makeRoutable", Array(lat, lon))
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Than you, Erel, I took your code and wrote this:

B4X:
#AdditionalJar: navappclient

Sub Process_Globals
    'none
End Sub

Sub Globals
'none
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim Factory As JavaObject
Dim client As JavaObject = Factory.InitializeStatic("com.tomtom.navapp.NavAppClient.Factory").RunMethod("make", Array(GetContext, Null))
Dim lat = 33, lon = 33 As Double
Dim r As JavaObject = client.RunMethod("makeRoutable", Array(lat, lon))
'    ToastMessageShow(r,True)
End Sub
Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

Sub GetContext As JavaObject
  Return GetBA.GetField("context")
End Sub


It throws a java error:
an error has occured in sub:
java.lang.reflect.InvocationTargetException
Thank you for your support, Georg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Here are the messages from Fast Debugger
Installing file.

PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
Error occurred on line: 42 (main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:109)
at b4a.example.main._activity_create(main.java:339)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5136)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:744)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Listener is null
at com.tomtom.navapp.NavAppClient$Factory.make(NavAppClient.java:125)

... 24 more
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is the relevant error: Caused by: java.lang.IllegalArgumentException: Listener is null

Seems like it doesn't allow you to pass Null for the error callback.


Try this:
B4X:
Dim Factory As JavaObject
Factory.InitializeStatic("com.tomtom.navapp.NavAppClient.Factory")
Dim callback As Object = Factory.CreateEvent("com.tomtom.navapp.internals.Callback", "NavError", Null)
Dim client As JavaObject = Factory.RunMethod("make", Array(GetContext, callback))
Dim lat = 33, lon = 33 As Double
Dim r As JavaObject = client.RunMethod("makeRoutable", Array(lat, lon))
End Sub

Sub NavError_Event (MethodName As String, Args() As Object) As Object
   Log(MethodName)
End Sub
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
The old error is gone. However, there is still an error:

PackageAdded: package:b4a.example
Installing file.

Error occurred on line: 43 (main)
java.lang.RuntimeException: Method: make not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:107)
at b4a.example.main._activity_create(main.java:343)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5136)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:744)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

** Activity (main) Resume **
I guess only the first line is relevant.
Line 43 is the underlined one in the Sub GetBa. GetContext in the ToastMessageShow works ok and the context is displayed, but not if its called from the second call in Factory.RunMethod


B4X:
Sub Activity_Create(FirstTime As Boolean)
ToastMessageShow (GetContext,True)
Dim Factory As JavaObject
Factory.InitializeStatic("com.tomtom.navapp.NavAppClient.Factory")
Dim callback As Object = Factory.CreateEvent("com.tomtom.navapp.internals.Callback", "NavError", Null)
Dim client As JavaObject = Factory.RunMethod("make", Array(GetContext, callback))
Dim lat = 33, lon = 33 As Double
Dim r As JavaObject = client.RunMethod("makeRoutable", Array(lat, lon))
End Sub

Sub NavError_Event (MethodName As String, Args() As Object) As Object
  Log(MethodName)
End Sub
Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

Sub GetContext As JavaObject
  Return GetBA.GetField("context")
End Sub

Thankful for your help, Georg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Okay, we are get further down. Now its
java.lang.RuntimeException: Method: makeRoutable not found in: com.tomtom.navapp.internals.NavAppClientImpl
thrown by the statement that begins with an Dim r...
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
I found the last error, it should be "makeRouteable" instead of "makeRoutable". I get back 33 as latitude and 33 longitude in r rather than my actual coordinates, but maybe the GPS is not ready to pass coordinates.
Thank you so much, Erel
Georg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Erel, may I just ask another question:
To call the method "getCurrentLocation" in the following interface

public interface LocationManager



NavApp LocationManager interface. For getting and displaying geographic locations.



Nested Class Summary
static interface LocationManager.DisplayLocationListener
Listener to receive information about displaying a location.


static class LocationManager.DisplayLocationResult

Result codes for displaying locations on the map.

Method Summary
void getCurrentLocation(Routeable.Listener listener)

Get the current GPS location.


How would you code that? Sorry for my total lack of Java knowhow (thats why I love b4A)
Georg
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim locationManager As JavaObject = client.RunMethod("getLocationManager", Null)
Dim RoutableListener As JavaObject = locationManager.CreateEvent("com.tomtom.navapp.Routeable.Listener", "Listener", null)
locationManager.RunMethod("getCurrentLocation", RoutableListener)

Sub Listener_Event (MethodName As String, Args() As Object) As Object

 Dim routable As JavaObject = Args(0)
 Log(routable.RunMethod("getLatitude", null))

End Sub
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Thank you so much, Erel, with all your help I think even I will finally get the grips on this java library business! I hope thsi is the last problem:

The code throws an exception at the line locationManager.RunMethod....
java.lang.ClassCastException: $Proxy7 cannot be cast to java.lang.Object[]

Georg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
I have uncommented this line above:
Dim r1 As JavaObject = client.RunMethod("makeRouteable", Array(lat, lon))
and now I get a different error message,
java.lang.IllegalArgumentException: argument 2 should have type java.lang.Object[], got $Proxy8
still on the same line:
locationManager.RunMethod("getCurrentLocation", RouteableListener)
Maybe that is significant
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…