Android Question [SOLVED] Reflection - NoSuchMethodException: remapCoordinateSystem

JackKirk

Well-Known Member
Licensed User
Longtime User
Hi,

I'm trying to work out how to get the compass direction of the device Z axis (aka "camera axis" etc).

I am trying to emulate the logic of:

http://stackoverflow.com/questions/...n-is-different-depending-on-phone-orientation

with:
B4X:
Sub compass_Z(compass_X() As Float) As Float()
'http://stackoverflow.com/questions/8625804/compass-direction-is-different-depending-on-phone-orientation
 
    Private wrk1Matrix(9), wrk2Matrix(9), result(3) As Float
  
    wrk1Matrix = getRotationMatrixFromOrientation(compass_X)
  
    Private SM_AXIS_X, SM_AXIS_Z As Int
  
    SM_AXIS_X = reflect.GetStaticField("android.hardware.SensorManager", "AXIS_X")
    SM_AXIS_Z = reflect.GetStaticField("android.hardware.SensorManager", "AXIS_Z")

    reflect.RunStaticMethod("android.hardware.SensorManager", "remapCoordinateSystem", Array As Object(wrk1Matrix, SM_AXIS_X, SM_AXIS_Z, wrk2Matrix), Array As String("[F", "[I", "[I", "[F"))
  
    reflect.RunStaticMethod("android.hardware.SensorManager","getOrientation", Array As Object(wrk2Matrix, result), Array As String("[F", "[F"))

    Return result

End Sub
which is dying with:
B4X:
main_compass_z (java line: 653)
java.lang.NoSuchMethodException: remapCoordinateSystem [class [F, class [I, class [I, class [F]
    at java.lang.Class.getDeclaredMethod(Class.java:635)
    at anywheresoftware.b4a.agraham.reflection.Reflection.RunStaticMethod(Reflection.java:917)
    at b4a.example.main._compass_z(main.java:653)
    at b4a.example.main._calculatefusedorientationtask_tick(main.java:587)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5832)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
at the statement:

reflect.RunStaticMethod("android.hardware.SensorManager", "remapCoordinateSystem", Array As Object(wrk1Matrix, SM_AXIS_X, SM_AXIS_Z, wrk2Matrix), Array As String("[F", "[I", "[I", "[F"))

I have used reflect.RunStaticMethod("android.hardware.SensorManager",... elsewhere without problems and I know remapCoordinateSystem is a valid method in android.hardware.SensorManager

So I'm guessing the types array Array As String("[F","[I","[I","[F") is the problem - but what? - I have run out of ideas.

Any help appreciated...
 

JackKirk

Well-Known Member
Licensed User
Longtime User
Another example of why it would be nice to be able to delete a thread.

If I change:

reflect.RunStaticMethod("android.hardware.SensorManager", "remapCoordinateSystem", Array As Object(wrk1Matrix, SM_AXIS_X, SM_AXIS_Z, wrk2Matrix), Array As String("[F", "[I", "[I", "[F"))

to:

reflect.RunStaticMethod("android.hardware.SensorManager", "remapCoordinateSystem", Array As Object(wrk1Matrix, SM_AXIS_X, SM_AXIS_Z, wrk2Matrix), Array As String("[F", "I", "I", "[F")) - i.e. [I => I

or:

reflect.RunStaticMethod("android.hardware.SensorManager", "remapCoordinateSystem", Array As Object(wrk1Matrix, SM_AXIS_X, SM_AXIS_Z, wrk2Matrix), Array As String("[F", "java.lang.int", "java.lang.int", "[F")) - i.e. [I => java.lang.int (and it is int NOT Int)

then it works - and all of this would have been fairly obvious if I had RTFM'd:

https://www.b4x.com/android/help/reflection.html

Apologies...
 
Last edited:
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

Just when I got Reflection sorted - now I'll have a go at JavaObject!

Thanks...
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
@JackKirk Sorry for bumping in on this old thread, but could you also share your code for getRotationMatrixFromOrientation? I am trying to accomplish the same.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Martin,

6 years - this was out of a hobby project I was working on at the time (Compleat Pocket Surveyor) that I never got round to publishing because I got involved in a real major project.

I looked through that project for "getRotationMatrixFromOrientation" and found this B4A translation of it:

B4X:
'************************************************************************************
'
'This procedure gets a rotation matrix from an orientation array
'
'Input parameters are:
'
'       Orient = orientation array of 3 values (x, y, z)
'
'Returns:
'
'       Rotation matrix
'
'Notes on this procedure:
'
'       o None
'
'************************************************************************************
Private Sub SFE_Rotation_From_Orientation(Orient() As Float) As Float()
       
    Private xM(9), yM(9), zM(9), result(9) As Float
    Private sinX, cosX, sinY, cosY, sinZ, cosZ As Float
 
    sinX = Sin(Orient(1))
    cosX = Cos(Orient(1))
    sinY = Sin(Orient(2))
    cosY = Cos(Orient(2))
    sinZ = Sin(Orient(0))
    cosZ = Cos(Orient(0))
 
    'Create rotation matrix about x-axis (pitch)
    xM(0) = 1: xM(1) = 0: xM(2) = 0
    xM(3) = 0: xM(4) = cosX: xM(5) = sinX
    xM(6) = 0: xM(7) = -sinX: xM(8) = cosX
   
    'Create rotation matrix about y-axis (roll)
    yM(0) = cosY: yM(1) = 0: yM(2) = sinY
    yM(3) = 0: yM(4) = 1: yM(5) = 0
    yM(6) = -sinY: yM(7) = 0: yM(8) = cosY
   
    'Create rotation matrix about z-axis (azimuth)
    zM(0) = cosZ: zM(1) = sinZ: zM(2) = 0
    zM(3) = -sinZ: zM(4) = cosZ: zM(5) = 0
    zM(6) = 0: zM(7) = 0: zM(8) = 1
   
    'Multiply rotation matrices together, order is y, x, z (roll, pitch, azimuth)
    result = SFE_Matrix_Multiplication(xM, yM)
    result = SFE_Matrix_Multiplication(zM, result)
   
    Return result
   
End Sub

Good luck - it would be no point in asking me any questions about it because it is all ancient history to me.
 
Upvote 0
Top