Android Question java.lang.ClassCastException: java.lang.String cannot be cast to b4a.example.preferencesdialog$_b4xprefitem

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Trying out the B4XPreferencesDialog and getting an error when the json file holds this:

{
"Version": 1.66,
"Theme": "Dark Theme",
"Items": [
{
"title": "Distance Point 1",
"type": "Text",
"key": "Distance_Point1",
"required": false
},
{
"title": "Distance Point 2",
"type": "Text",
"key": "Distance_Point2",
"required": false
}
]
}

The error occurs when clicking the OK button after having put 2 texts in the 2 text boxes.

I can reproduce this in the slightly altered example project attached to the first post from Erel about this library.
I attached that project.

RBS
 

Attachments

  • Project.zip
    195.2 KB · Views: 49

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I don't get any error...

Please post the full error message
I changed from B4XLibrary to a project with a standard class (so I can locate the error).

This is the full error text:

Copying updated assets files (20)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 753 (clsPreferencesDialog)
java.lang.ClassCastException: java.lang.String cannot be cast to b4a.example.clspreferencesdialog$_b4xprefitem
at b4a.example.clspreferencesdialog._scrolltoitemwitherror(clspreferencesdialog.java:1016)
at b4a.example.b4xmainpage._prefdialog_isvalid(b4xmainpage.java:553)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1114)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:1069)
at b4a.example.clspreferencesdialog._commitchanges(clspreferencesdialog.java:1681)
at b4a.example.clspreferencesdialog$ResumableSub_ShowDialog.resume(clspreferencesdialog.java:735)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:150)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.keywords.Common$15.run(Common.java:1804)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:230)
at android.os.Looper.loop(Looper.java:319)
at android.app.ActivityThread.main(ActivityThread.java:8918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
** Activity (main) Pause event (activity is not paused). **

And this is where the error occurs:

'Scrolls to the item and shows a "shake" animation.
Public Sub ScrollToItemWithError (key As String)
For i = 0 To CustomListView1.Size - 1
Dim Item As CLVItem = CustomListView1.GetRawListItem(i)
Dim PrefItem As B4XPrefItem = Item.Value '<<<< error occurs here
If PrefItem.Key = key Then
GoToItem(i)
Return
End If
Next
End Sub

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Where does clsPreferencesDialog come from? The library class is named PreferencesDialog.
I changed it from a B4X library to a project with a standard class instead (so I can locate the error).
I called the class clsPreferencesDialog.
I can see now that the problem is caused by the texts being evaluated as a number.

Doing this makes it work OK, but I understand that is not the way to solve this:

Private Sub PrefDialog_IsValid (TempData As Map) As Boolean
Return True
Dim number As Int = TempData.GetDefault("NumberToValidate", 0)
If number < 10 Or number > 20 Then
prefdialog.ScrollToItemWithError("NumberToValidate")
Return False
End If
Return True
End Sub

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I changed it from a B4X library to a project with a standard class instead (so I can locate the error).
I called the class clsPreferencesDialog.
I can see now that the problem is caused by the texts being evaluated as a number.

Doing this makes it work OK, but I understand that is not the way to solve this:

Private Sub PrefDialog_IsValid (TempData As Map) As Boolean
Return True
Dim number As Int = TempData.GetDefault("NumberToValidate", 0)
If number < 10 Or number > 20 Then
prefdialog.ScrollToItemWithError("NumberToValidate")
Return False
End If
Return True
End Sub

RBS
Perhaps changing to this in Sub CommitChanges is a solution:

If PrefItem.ItemType = TYPE_NUMERICRANGE Then
If mEventName <> "" And xui.SubExists(mCallback, mEventName & "_IsValid", 1) Then
Dim Valid As Boolean = CallSub2(mCallback, mEventName & "_IsValid", Temp)
If Valid = False Then Return False
End If
End If

RBS
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I changed it from a B4X library to a project with a standard class instead (so I can locate the error).
I called the class clsPreferencesDialog.
I can see now that the problem is caused by the texts being evaluated as a number.
1. No need to change the class name. You can add the module. It will override the module inside the library.

2. Please use [code]code here...[/code] tags when posting code.

The error is not in this code. Try to reproduce it with the unmodified library and upload the project.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
1. No need to change the class name. You can add the module. It will override the module inside the library.

2. Please use [code]code here...[/code] tags when posting code.

The error is not in this code. Try to reproduce it with the unmodified library and upload the project.
>>
Try to reproduce it with the unmodified library and upload the project.

That is exactly what I did in the first post.
Will look at it again.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Re-downloaded the latest B4XPreferencesDialog.b4xlib.
Redownloaded the example project as in the first posting by Erel about this b4xlib.
As I had serious trouble updating the project json file (kept reverting to the original json) I renamed that to settings.json.
Made some minor alterations (4 lines commented out) in B4XMainPage.
Zipped the project as instructed at the top of B4XMainPage.

Attached that zip file.

Still exactly the same error:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
preferencesdialog_scrolltoitemwitherror (java line: 1596)
java.lang.ClassCastException: java.lang.String cannot be cast to b4a.example.preferencesdialog$_b4xprefitem
at b4a.example.preferencesdialog._scrolltoitemwitherror(preferencesdialog.java:1596)
at b4a.example.b4xmainpage._prefdialog_isvalid(b4xmainpage.java:397)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1114)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:1069)
at b4a.example.preferencesdialog._commitchanges(preferencesdialog.java:518)
at b4a.example.preferencesdialog$ResumableSub_ShowDialog.resume(preferencesdialog.java:2087)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.keywords.Common$15.run(Common.java:1804)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:230)
at android.os.Looper.loop(Looper.java:319)
at android.app.ActivityThread.main(ActivityThread.java:8918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

As explained I have a fix for this problem, but have no idea why Erel doesn't get the same error.

RBS
 

Attachments

  • Project.zip
    182.7 KB · Views: 42
Upvote 0

teddybear

Well-Known Member
Licensed User
The issue is that you added a PrefDialog_IsValid event to check the key NumberToValidate, but in your settings.txt there is no such key. comment this line
B4X:
'prefdialog.SetEventsListener(Me, "PrefDialog")
 
Last edited:
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
The issue is that you added a PrefDialog_IsValid event to check the key NumberToValidate, but in your settings.txt there is no such key. comment this line
B4X:
'prefdialog.SetEventsListener(Me, "PrefDialog")
Ah, OK, thanks for that.
I take it that if you want to evaluate a number then the json item needs to be a numeric range, so I take my suggested fix was OK.

RBS
 
Upvote 0
Top