Android Question Passing a variable to a Class module in a b4xlib file

synasir

Member
Licensed User
Longtime User
Hi, I am using the Open Street Map viewer b4xlib file. It uses "tiles.db3" to store all the downloaded map tiles in one of the class modules. How do I change the filename to something else during the running of the program?

fcvMap.setfilename("JOHOR") is triggering setFileName but not passing it to fMapTileManager.Initialize(NameY)


B4X:
#region initialization
Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback
    fMap.Initialize
    fMap.fShapes.Initialize
    fLastTouch.Initialize
    fStartTouch.Initialize
    fMapTileManager.Initialize(NameY)
    
End Sub

public Sub setFileName(NameX As String)
    NameY = NameX   
    Log(NameY)
End Sub

Thanks.
 

mangojack

Well-Known Member
Licensed User
Longtime User
The way I see it fMapTileManager has already been initialized (an in turn initialized the db) before you call setFileName.

You could possibly shift thefMapTileManager.Initialize to the setFileName sub

What changes have you made to the fMapTileManager class ... ? ( Initialize sub, FileName variable , InitDB sub etc)

B4X:
Public Sub Initialize
    dbInit
End Sub

'open db, create it if necessary
Private Sub dbInit
    fXUI.SetDataFolder("")
    If Not(File.Exists(fXUI.DefaultFolder,"tiles.db3")) Then
        #if B4A or B4i
            fDB.Initialize(fXUI.DefaultFolder,"tiles.db3",True)
            '........................................

.
 
Upvote 0

synasir

Member
Licensed User
Longtime User
B4X:
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(FN as string)
  Fname = FN
    dbInit
End Sub


'open db, create it if necessary
Private Sub dbInit
    fXUI.SetDataFolder("")
    If Not(File.Exists(fXUI.DefaultFolder,Fname & ".db3")) Then
        #if B4A or B4i
            fDB.Initialize(fXUI.DefaultFolder,Fname & ".db3",True)
        #else if B4J
            fDB.InitializeSQLite(fXUI.DefaultFolder,Fname & ".db3",True)
        #end if
        fDB.ExecNonQuery("CREATE TABLE t_tiles(tiZ INTEGER NOT NULL,tiX INTEGER NOT NULL,tiY INTEGER NOT NULL,tiPNG BLOB NOT NULL, primary key (tiZ,tiX,tiY))")
    Else
        #if B4A or B4i
        fDB.Initialize(fXUI.DefaultFolder,Fname & ".db3",False)
        #else if B4J
            fDB.InitializeSQLite(fXUI.DefaultFolder,Fname & ".db3",False)
        #End If
    End If
End Sub
 
Upvote 0

synasir

Member
Licensed User
Longtime User
The way I see it fMapTileManager has already been initialized (an in turn initialized the db) before you call setFileName.

You could possibly shift thefMapTileManager.Initialize to the setFileName sub

What changes have you made to the fMapTileManager class ... ? ( Initialize sub, FileName variable , InitDB sub etc)

B4X:
Public Sub Initialize
    dbInit
End Sub

'open db, create it if necessary
Private Sub dbInit
    fXUI.SetDataFolder("")
    If Not(File.Exists(fXUI.DefaultFolder,"tiles.db3")) Then
        #if B4A or B4i
            fDB.Initialize(fXUI.DefaultFolder,"tiles.db3",True)
            '........................................

.
If I shift the fMapTileManager.Initialize to the setFileName sub, it will error out.


b4xpagesmanager._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1 (java line: 312)
java.lang.RuntimeException: java.lang.NullPointerException
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:523)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:469)
at b4j.example.b4xpagesmanager._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1(b4xpagesmanager.java:312)
at b4j.example.b4xpagesmanager._vvv2(b4xpagesmanager.java:727)
at b4j.example.b4xpagesmanager._v7(b4xpagesmanager.java:118)
at b4j.example.b4xpagesmanager._v0(b4xpagesmanager.java:125)
at b4j.example.b4xpagesmanager._initialize(b4xpagesmanager.java:496)
at b4j.example.main._appstart(main.java:63)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at b4j.example.main.start(main.java:37)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NullPointerException
at b4j.example.cvmap._setvvvvvvvvvvvvvvvvvvvvvvvv7(cvmap.java:1311)
at b4j.example.b4xmainpage._b4xpage_created(b4xmainpage.java:90)
at b4j.example.b4xmainpage.callSub(b4xmainpage.java:1315)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:498)
... 23 more
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
It seems to work Ok here... But I notice the sample app is Not B4XPages project .

B4X:
'Sample App Main activity ...
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    fcvMap.setFileName("JOHOR")


'cvMap Class ...
Public Sub setFileName(NameX As String)
    NameY = NameX
    fMapTileManager.Initialize(NameY)
End Sub


'clMapTileManager Class ...
Public Sub Initialize(Fn As String)
    Fname = Fn
    Log("Initializing Class > db filename = " & Fn)  'Log  >>> Initializing Class > db filename = JOHOR
 
    dbInit
End Sub

'open db, create it if necessary
Private Sub dbInit
    fXUI.SetDataFolder("")
    If Not(File.Exists(fXUI.DefaultFolder,Fname & ".db3")) Then
        #if B4A or B4i
            fDB.Initialize(fXUI.DefaultFolder,Fname &".db3",True)
        #else if B4J
            fDB.InitializeSQLite(fXUI.DefaultFolder,Fname".db3",True)
        #end if
        fDB.ExecNonQuery("CREATE TABLE t_tiles(tiZ INTEGER NOT NULL,tiX INTEGER NOT NULL,tiY INTEGER NOT NULL,tiPNG BLOB NOT NULL, primary key (tiZ,tiX,tiY))")
    Else
        #if B4A or B4i
        fDB.Initialize(fXUI.DefaultFolder,Fname &".db3",False)
       
    Log("Initializing DB > db file name = " & Fname & ".db3")  'Log  >>> Initializing DB > db filename = JOHOR.db3
   
'...

Without knowing exactly where you are declaring classes / lib and calling .SetFileName its a little hard to help further.
 
Last edited:
Upvote 0

synasir

Member
Licensed User
Longtime User
I am using the B4A class library here.


Nothing have been changed except trying to use a different file name programmatically.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
I have just tested the B4XPages example with the exact same changes as above which also successfully changed the Database name. with no error.

I am presuming you unzipped the B4XMap.b4xlib , made your changes to the 2 classes and rezipped the lib files.

Maybe make the same changes to a fresh copy of the sample app and retest. If it still fails then upload it.
 
Upvote 0

synasir

Member
Licensed User
Longtime User
I am using B4J

Here is the zipped file. I changed the relevant files in B4XMap.b4xlib and renamed it to B4XMapTest.b4xlib.

In the cvMap.bas file, I have commented on the issues and errors.

Thanks.


B4X:
#region initialization
Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback
    fMap.Initialize
    fMap.fShapes.Initialize
    fLastTouch.Initialize
    fStartTouch.Initialize
    fMapTileManager.Initialize(NameY) 'No error but blank filename - .db3
        
End Sub

public Sub setFileName(NameX As String)
    NameY = NameX       
    'fMapTileManager.Initialize(NameX) 'Error Out
End Sub
 

Attachments

  • B4J.ZIP
    36.3 KB · Views: 34
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Firstly ... you have posted a question in the Android forum and then submit a B4J example .. ?

Anyway , call SetFileName after you have loaded your layout.
B4X:
Root = Root1
Root.LoadLayout("main")
 
fcvMap.SetFileName("TEST")

and here is a rezipped b4xlib (I have edited the class variables slightly .. DBFileName , instead of X,Y etc.)

Both b4j & b4a versions work with the new lib as far as Initializing the db with new name.
 

Attachments

  • B4Xmap.b4xlib
    23.8 KB · Views: 35
Last edited:
Upvote 0

synasir

Member
Licensed User
Longtime User
Firstly ... you have posted a question in the Android forum and then submit a B4J example .. ?

Anyway , call SetFileName after you have loaded your layout.
B4X:
Root = Root1
Root.LoadLayout("main")
 
fcvMap.SetFileName("TEST")

and here is a rezipped b4xlib (I have edited the class variables slightly .. DBFileName , instead of X,Y etc.)

Both b4j & b4a versions work with the new lib as far as Initializing the db with new name.
Actually, I was working on both versions. Anyway, thanks for the help.
 
Upvote 0
Top