Android Question [SOLVED] Cannot cast array of CSBuilder to array of CharSequence

Toky Olivier

Active Member
Licensed User
Longtime User
I use Corwin42 Preferences library and I tried this code.

B4X:
        Dim lstSigneZodiaque As ListPreference
    Dim dummy As CSBuilder
    dummy.Initialize
    Dim cs() As CSBuilder = Array As CSBuilder(dummy,dummy,dummy)
    Dim ty As Typeface
    ty = Typeface.LoadFromAssets("velezodiac.ttf")
    cs(0).Initialize.Typeface(ty).Append("P Y S Q T").PopAll
    cs(1).Initialize.Typeface(ty).Append("p y s q t").PopAll
    cs(2).Initialize.Typeface(ty).Append("B K E C F").PopAll
    lstSigneZodiaque = cat1.AddListPreference("","IconeSigneZodiaque", "Kisary mifanaraka amin'ny andro","Misafidiana anaty lisitra", "0", Array As String("0", "1", "2"), cs)

I get error bellow at runtime:
preferences_prefview_ready (java line: 456)
java.lang.ClassCastException: anywheresoftware.b4a.objects.CSBuilder[] cannot be cast to java.lang.CharSequence[]
at mg.rtsoft.alimanaka.preferences._prefview_ready(preferences.java:456)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:163)
at de.amberhome.preferences.PreferenceViewWrapper$MyPreferenceFragment.onViewCreated(PreferenceViewWrapper.java:253)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)



It is said that "anywheresoftware.b4a.objects.CSBuilder[] cannot be cast to java.lang.CharSequence[]". How can I solve this problem please?

Thank you in advance.
 

Toky Olivier

Active Member
Licensed User
Longtime User
If I use this code:
B4X:
Dim lstSigneZodiaque As ListPreference
    Dim dummy As CSBuilder
    dummy.Initialize
    Dim cs() As CSBuilder = Array As CSBuilder(dummy,dummy,dummy)
    Dim ty As Typeface
    ty = Typeface.LoadFromAssets("velezodiac.ttf")
    cs(0).Initialize.Typeface(ty).Append("P Y S Q T").PopAll
    cs(1).Initialize.Typeface(ty).Append("p y s q t").PopAll
    cs(2).Initialize.Typeface(ty).Append("B K E C F").PopAll
    Dim cs2() As String = Array As String(cs(0).ToString, cs(1).ToString, cs(2).ToString)
    lstSigneZodiaque = cat1.AddListPreference("","IconeSigneZodiaque", "Kisary mifanaraka amin'ny andro","Misafidiana anaty lisitra", "0", Array As String(0, 1, 2), cs2)

It's OK but Typeface is not changed. It's normal because I passed an array of string.

What I don't understand, Erel said in this tutorial that "CSBuilder is similar to StringBuilder. Instead of building strings, it builds CharSequences that include style information." So why I got error with that code...

The prototype of the AddListPreference function is:
B4X:
AddListPreference(EventName As String, Key As String, Title As CharSequence, Summary As CharSequence, Default As String, Values() As CharSequence, Texts() As CharSequence) As ListPreference
 
Upvote 0

Toky Olivier

Active Member
Licensed User
Longtime User
You can create an array of CharSequences with this code:
Code:
Dim charSequences AsJavaObject
charSequences.InitializeArray("java.lang.CharSequence", Array(cs(0), cs(1), cs(2)))

That works. Thank you @Erel. I need to learn more JavaObject object.
 
Upvote 0
Top