Android Tutorial KeyValueStore class - Simple and efficient key/value data store

Status
Not open for further replies.

luke2012

Well-Known Member
Licensed User
Longtime User
It is recommended to move all the "non-ui" objects to Process Globals instead of Globals and only initialize them when FirstTime is True.

I don't see the problem in your code. However try to create a simple program that reproduces the same issue.

Ok I follow your suggestion. On which B4A version and Android version did you try the code?
 

luke2012

Well-Known Member
Licensed User
Longtime User
in the current implementation is possible to retrieve a list with all values stored in the KeyValueStore?
 

boten

Active Member
Licensed User
Longtime User
Strange thing with kvs:
if I Getobject of type list/map and the key is not in the kvs, then an uninitialised object is returned, that's ok. If i use this object later an error should occur.
the thing is that some users report they get "uninitialised" error, many don't. Myself, i can't reproduce this error on any of my 4 devices (galaxy s, galaxy s4, asus tablet and a noname tablet) even though i follow the actions that complaining users do. Any ideas?
 

boten

Active Member
Licensed User
Longtime User
You will get the uninitialized error when every you call a method of an uninitialized object. The solution should be to avoid fetching an invalid key.
I know that, and I know I should check for uninitialized objects. I used unintialised. I was wondering why the error occurs to some users and not to others, even tho the sequence of actions should lead to the same usage.
 

Shadow&Max

Active Member
Licensed User
Longtime User
Sorry to be dense here, but I'm new to B4A, but learning very fast...

I'm trying to get this to run, and it's erroring out on the emulator, even with the simplest of two lines...

The two sections of code:

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim HoldingString As String
Dim Message1 As String

Private kvs As KeyValueStore

End Sub


Sub Activity_Create(FirstTime AsBoolean)
'Do not forget to load the layout file created with the visual designer. For example:
If FirstTime Then
kvs.Initialize(File.DirDefaultExternal, "agsam")
End If​

Just running this alone generates the Log:

** Activity (main) Create, isFirst = true **
Error occurred on line: 10 (keyvaluestore)
mainafterFirstLayout (java line: 98)
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at anywheresoftware.b4a.sql.SQL.Initialize(SQL.java:37)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:520)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:235)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at com.twodogapps.myfirsttest.main.afterFirstLayout(main.java:98)
at com.twodogapps.myfirsttest.main.access$100(main.java:16)
at com.twodogapps.myfirsttest.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **

I know this has something to do with opening the file... What am I doing wrong here? I need this and am really stuck! And I've tried other file names including "datastore". Fails every time, and right now don't have an actual device I can test on other than the emulator. Help please! Thanks in advance...
 
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
I think I have it now... DirInternal? Seems to work without erroring out... More in a bit...

As Emily Litella said... "Nevermind!"

GOT IT working... am tickled pink!
 
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
Thanks Erel...

In Android programming, where's the best place to save what should end up being a moderate sized file? Internal or External, and does External mean the SD card?
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…