Android Question Class Exception Error

Bill Kantz

Member
Licensed User
Longtime User
Here is the code:

B4X:
Sub Process_Globals

    Type StrLoc (City As String, State As String, Lat As Double, Lon As Double)
    Dim LocLst As List

End Sub

Sub Globals


    Private LvLoc As ListView
    Dim x As Int

End Sub

Sub Activity_Create(FirstTime As Boolean)

    
    Activity.LoadLayout("Sto_Loc")


End Sub

Sub Activity_Resume

    LocLst.Initialize
    LvLoc.Clear
    LvLoc.AddSingleLine("Use GPS for Location")
    LvLoc.AddSingleLine("Add New Location")
    If File.Exists(File.DirInternal, "StoLoc.Lst") Then
        LocLst = File.ReadList(File.DirInternal, "StoLoc.Lst")
        Log("LocLst  " & LocLst)
        For x = 0 To LocLst.Size - 1
            Dim LocTmp As StrLoc
            LocTmp = LocLst.Get(x)
            LvLoc.AddSingleLine(LocTmp.City&",  "& LocTmp.State)
        Next
    End If


The beginning og the log is a log(LocLst) after the file read

B4X:
** Activity (stoloc) Resume **
LocLst  (ArrayList) [[City=Paris, State=Texas, Lat=33.6609389, , Lon=-95.555513, IsInitialized=true], [City=Paris, State=Texas, Lat=33.6609389, , Lon=-95.555513, IsInitialized=true], [City=Paris, State=Tennessee, Lat=36.3020023, , Lon=-88.3267107, IsInitialized=true], [City=Paris, State=Illinois, Lat=39.611146, , Lon=-87.6961374, IsInitialized=true], [City=Paris, State=Kentucky, Lat=38.2097987, , Lon=-84.2529869, IsInitialized=true], [City=Clinton, State=Massachusetts, Lat=42.4167635, , Lon=-71.6829081, IsInitialized=true]]
stoloc_activity_resume (java line: 393)
java.lang.ClassCastException: java.lang.String cannot be cast to com.sailawayapps.bestwx.stoloc$_strloc
    at com.sailawayapps.bestwx.stoloc._activity_resume(stoloc.java:393)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at com.sailawayapps.bestwx.stoloc$ResumeMessage.run(stoloc.java:298)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    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:790)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.ClassCastException: java.lang.String cannot be cast to com.sailawayapps.bestwx.stoloc$_strloc

The line causing the error:LocTmp = LocLst.Get(x)

I can not figure out why I am getting the cast error?

Bill
 
Last edited:

Roycefer

Well-Known Member
Licensed User
Longtime User
You are reading a text file into a List. This makes the LocLst a list of Strings, not a list of StrLoc objects. You'll need to parse the String and set the StrLoc's values to the values described in the parsed String.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
It looks like it has a ".lst" extension. Did you use File.WriteList() to create the file? Then it's a text file, regardless of the extension. Anyhow, that doesn't really matter because you're using File.ReadList() to read the file and that will return a List of Strings; each line in the file a String item in the List. And we know that it's doing that because the app is failing when it tries to cast a String to a StrLoc object. You have to do the conversion from String to StrLoc yourself. This will be a simple matter of successive Regex.Split() calls. Try logging LocLst.Get(x) so you can see what it looks like.
 
Upvote 0

Bill Kantz

Member
Licensed User
Longtime User
Thanks for your help. I confused read list and write list with random access files which actually read/write the objects.
 
Upvote 0
Top