Android Question Error: java.lang.object cannot be cast to b4xpagesmanager (KeyValueStore and B4XPages)

asales

Expert
Licensed User
Longtime User
I get this error in the line below in my Alarm module receiver in a B4XPage Project.
B4X:
Dim horanot As Double

If B4XPages.MainPage.kvs.GetDefault("hour_1", "08:00").As(String).Length > 1 Then
  horanot = Starter.kvs.GetDefault("hour_1", "08:00").As(String).SubString2(0,2)
End If

Dim t As Long
t = FindNextTime(Array As Double(horanot,12,18))
It occours when I check if the hour is setting in the KeyValueStore to the scheduling alarm.

I initializated the KVS in the B4XMainPage.

Due this information from Erel:
"Don't try to access B4XPages from FirebaseMessaging service. B4XPages can only be created after the main activity was created."

I tried to initializated the KVS in the Starter and get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor anywheresoftware.b4a.sql.SQL.ExecQuery2(java.lang.String, java.lang.String[])' on a null object reference

It not happens in an Activity project.

How can I check the values setting the the KVS in the Alarm module in a B4XPages project?
 

asales

Expert
Licensed User
Longtime User
You need to move kvs to the starter service.
I tried to initializated the KVS in the Starter and get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor anywheresoftware.b4a.sql.SQL.ExecQuery2(java.lang.String, java.lang.String[])' on a null object reference

It not happens in an Activity project.
So, I put a copy this code in the Alarm service (and in the Starter Service)
B4X:
xui.SetDataFolder("kvs")
kvs.Initialize(xui.DefaultFolder, "kvs.dat")
and worked in my tests. It read from kvs when the Alarm service is started (and the Starter don't).
Could be a problem use in this way?
 
Upvote 0
Top