Reflector and TypeFace...

fiaful

Member
Licensed User
Longtime User
Hi there!

I'm writing a class to write text on canvas using layout ... but when I try to set the Typeface by reflector I get an error:

java.lang.NoSuchMethodException: setTypeface [class java.lang.String]

(of course! This is because setTypeface takes a parameter of type Typeface ... but I give it the Typeface of B4A!)

B4X:
Public Sub SetTypeface(Font As Typeface)
   Dim R As Reflector
   R.Target = TP ' TP is class private variable of type Object instanced with R.CreateObject("android.text.TextPaint")
   R.RunMethod2("setTypeface", Font, "android.graphics.Typeface")

End Sub

(all other methods I invoke on TP works correctly with int, float and string parameters... but not setTypeface!)

I also tried to take the object inside of the Typeface of B4A ... calling getObject or getObjectOrNull with:

B4X:
Public Sub SetTypeface(Font As Typeface)
   Dim R As Reflector
   R.Target = Font
   Dim RealTypeface As Object = R.RunMethod("getObject")
   R.Target = TP ' TP is class private variable of type Object instanced with R.CreateObject("android.text.TextPaint")
   R.RunMethod2("setTypeface", RealTypeface, "android.graphics.Typeface")
End Sub

and this is the returned error:

java.lang.NoSuchMethodException: getObject []

so... there is someone who knows how to properly pass the Typeface via reflector?

thanks in advance
 

agraham

Expert
Licensed User
Longtime User
Typeface is in fact an android.graphics.Typeface, the problem is with using RunMethod2 which expects String parameters. Try RunMethod4.
B4X:
Public Sub SetTypeface(Font As Typeface)
    Dim R As Reflector
    'R.Target = TP ' TP is class private variable of type Object instanced with R.CreateObject("android.text.TextPaint")
    R.Target = R.CreateObject("android.text.TextPaint")
    R.RunMethod4("setTypeface", Array As Object(Font), Array As String("android.graphics.Typeface"))
End Sub
 
Upvote 0
Top