Problem calling method with reflection

corwin42

Expert
Licensed User
Longtime User
I have a problem to call a method with reflection. I'm trying to display HTML Text in a label as described here (last example)

This is my code:
B4X:
Sub Globals
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Label1.Initialize("")
   Activity.AddView(Label1, 0, 0, 100%x, 100%y)
   
   Label1.TextSize = 16
   
   Dim r As Reflector
   Dim o As Object
   
   r.Target = Label1
   
   o = r.RunStaticMethod("android.text.method.LinkMovementMethod", "getInstance", Null, Null)
   r.Target=Label1
   r.RunMethod2("setMovementMethod", o, "android.text.method.MovementMethod")
   
   Dim vText As String
   vText = "<b>Please</b> go to <a href='http://www.google.de'>Google WebSite</a>!"
   
   Label1.Text = r.RunStaticMethod("android.text.Html", "fromHtml", Array As String(vText), Array As String("java.lang.String"))
End Sub

I get an IllegalArgumentException:

B4X:
main_activity_create (java line: 228)

java.lang.IllegalArgumentException: argument type mismatch
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:205)
   at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod2(Reflection.java:806)
   at de.amberhome.htmltest.main._activity_create(main.java:228)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
   at de.amberhome.htmltest.main.afterFirstLayout(main.java:84)
   at de.amberhome.htmltest.main.access$100(main.java:16)
   at de.amberhome.htmltest.main$WaitForLayout.run(main.java:72)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3683)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalArgumentException: argument type mismatch

I think the problem is that android.text.method.MovementMethod is an interface and android.text.method.MovementMethod.getInstance() returns an object of type LinkMovementMethod which implements this interface.

Any way around it with reflection library? Otherwise I will create a small helper library for this.
 
Top