I keep getting the following error which it seems is generated in B4XMainPage:
I do not know which JavaObject the error relates to?
My B4XMainPage code:
Stepping through the code with debug, it seems the error is related to:
and
B4X:
*** Service (starter) Create ***
android.intent.action.SEND
** Service (starter) Start **
** Activity (main) Create (first time) **
b4xmainpage_initialize (java line: 1067)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:285)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:138)
at com.ecowatch.isprotector.b4xmainpage._initialize(b4xmainpage.java:1067)
at com.ecowatch.isprotector.b4xpagesmanager._initialize(b4xpagesmanager.java:712)
at com.ecowatch.isprotector.main$ResumableSub_Activity_Create.resume(main.java:425)
at com.ecowatch.isprotector.main._activity_create(main.java:394)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at com.ecowatch.isprotector.main.afterFirstLayout(main.java:105)
at com.ecowatch.isprotector.main.access$000(main.java:17)
at com.ecowatch.isprotector.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7945)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I do not know which JavaObject the error relates to?
My B4XMainPage code:
B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=iS.zip
#ignorewarnings:12
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private HamburgerIcon As B4XBitmap
Public pgx2 As pg2
Public pgx5 As pg5
Public pgx3 As pg3
Public pgx4 As pg4
Public WobbleMenu1 As WobbleMenu
Private config As Map
Public dbUser As B4XDBUtils
Private ToastMessage As BCToast
Public Dbase As String = ""
Public FBKey As String
Public ActivePage As String
Private LastIntent As Intent
Public MP As MediaPlayer
Private lstDevicesHome As CustomListView
Private msgdevicesHome As B4XView
Private msgdatedevicesHome As B4XView
Private msgdoor As ImageView
Private msgvolvage As ImageView
Private lblTotDevices As B4XView
Private imgSOS As B4XView
Private imgdeviceicon As B4XView
Private analytics As FirebaseAnalytics
End Sub
'update the badge across the application
Sub UpdateBadge
Dim BadgeNumber As Int = modAVP.BadgeNumber
WobbleMenu1.RemoveBadge(2)
pgx5.WobbleMenu1.RemoveBadge(2)
pgx2.WobbleMenu1.RemoveBadge(2)
pgx3.WobbleMenu1.RemoveBadge(2)
pgx4.WobbleMenu1.RemoveBadge(2)
If BadgeNumber > 0 Then
WobbleMenu1.SetBadge(2, BadgeNumber, Colors.White, Colors.Blue)
pgx5.WobbleMenu1.SetBadge(2, BadgeNumber, Colors.White, Colors.Blue)
pgx2.WobbleMenu1.SetBadge(2, BadgeNumber, Colors.White, Colors.Blue)
pgx3.WobbleMenu1.SetBadge(2, BadgeNumber, Colors.White, Colors.Blue)
pgx4.WobbleMenu1.SetBadge(2, BadgeNumber, Colors.White, Colors.Blue)
End If
End Sub
'
'update the badge across the application
Sub UpdateDeviceCount
Dim BadgeNumber As Int = modAVP.DeviceNumber
WobbleMenu1.RemoveBadge(4)
pgx5.WobbleMenu1.RemoveBadge(4)
pgx2.WobbleMenu1.RemoveBadge(4)
pgx3.WobbleMenu1.RemoveBadge(4)
pgx4.WobbleMenu1.RemoveBadge(4)
If BadgeNumber > 0 Then
WobbleMenu1.SetBadge(4, BadgeNumber, Colors.White, Colors.Blue)
pgx5.WobbleMenu1.SetBadge(4, BadgeNumber, Colors.White, Colors.Blue)
pgx2.WobbleMenu1.SetBadge(4, BadgeNumber, Colors.White, Colors.Blue)
pgx3.WobbleMenu1.SetBadge(4, BadgeNumber, Colors.White, Colors.Blue)
pgx4.WobbleMenu1.SetBadge(4, BadgeNumber, Colors.White, Colors.Blue)
End If
End Sub
Sub ReadConfig As Map
Return File.ReadMap(File.DirAssets, "config.properties")
End Sub
Public Sub Initialize
B4XPages.GetManager.TransitionAnimationDuration = 0
B4XPages.GetManager.LogEvents = False
config = ReadConfig
Dbase = config.Get("Database")
FBKey = config.Get("FBKey")
modAVP.BadgeNumber = 0
HamburgerIcon = xui.LoadBitmapResize(File.DirAssets, "Logo-IS-HP.jpg", 60dip, 60dip, True)
'
MP.initialize2("mp")
Dim jo As JavaObject = MP
Dim p As Phone
jo.getfieldjo("mp").runmethod("setAudioStreamType", Array(p.VOLUME_NOTIFICATION))
MP.load(File.DirAssets, "alarm-15.mp3")
B4XPages.GetManager.ActionBar.RunMethod("setDisplayHomeAsUpEnabled", Array(True))
Dim bd As BitmapDrawable
bd.Initialize(HamburgerIcon)
B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(bd))
End Sub
'go home
Sub WobbleMenu1_Tab1Click
End Sub
Sub WobbleMenu1_Tab2Click
'If Starter.IsLoading Then
NavigateTo("pg2")
End Sub
Sub WobbleMenu1_Tab3Click
'If Starter.IsLoading Then Return
NavigateTo("pg3")
End Sub
Sub WobbleMenu1_Tab4Click
'If Starter.IsLoading Then Return
NavigateTo("pg4")
End Sub
Sub WobbleMenu1_Tab5Click
'If Starter.IsLoading Then Return
NavigateTo("pg5")
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
analytics.Initialize
Root = Root1
Root.LoadLayout("MainPage")
'you can add more topics here. Note that in B4i the topics will be prefixed with ios_.
CallSubDelayed2(FirebaseMessaging, "SubscribeToTopics", Array("is"))
'request notification permission
#if B4A
Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
If HasPermission = False Then
Log("no permission")
ToastMessageShow("no permission", True)
End If
#Else If B4i
Main.App.RegisterUserNotifications(True, True, True)
Main.App.RegisterForRemoteNotifications
#End If
ToastMessage.Initialize(Root)
B4XPages.SetTitle(Me, "PROTECTOR")
' Wait For Token_Ready
Dim FBToken As String = FirebaseMessaging.Token 'ignore
AddWobbleMenu(WobbleMenu1, 1)
WobbleMenu1.SetVisible(False, False)
#if B4i
Dim bb As BarButton
bb.InitializeBitmap(HamburgerIcon, "hamburger")
B4XPages.GetNativeParent(Me).TopLeftButtons = Array(bb)
#End if
'
#if B4J
Dim iv As ImageView
iv.Initialize("imgHamburger")
iv.SetImage(HamburgerIcon)
Drawer.CenterPanel.AddView(iv, 2dip, 2dip, 32dip, 32dip)
iv.PickOnBounds = True
#end if
#End Region
'
'initialize the page and add it to the collection
'ensure the layout is also added
pgx2.Initialize
B4XPages.AddPageAndCreate("pg2", pgx2)
pgx3.Initialize
B4XPages.AddPageAndCreate("pg3", pgx3)
pgx4.Initialize
B4XPages.AddPageAndCreate("pg4", pgx4)
pgx5.Initialize
B4XPages.AddPageAndCreate("pg5", pgx5)
End Sub
#if B4A
Private Sub CheckAndRequestNotificationPermission As ResumableSub
Dim p As Phone
If p.SdkVersion < 33 Then Return True
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim targetSdkVersion As Int = ctxt.RunMethodJO("getApplicationInfo", Null).GetField("targetSdkVersion")
If targetSdkVersion < 33 Then Return True
Dim NotificationsManager As JavaObject = ctxt.RunMethod("getSystemService", Array("notification"))
Dim NotificationsEnabled As Boolean = NotificationsManager.RunMethod("areNotificationsEnabled", Null)
If NotificationsEnabled Then Return True
Dim rp As RuntimePermissions
rp.CheckAndRequest(rp.PERMISSION_POST_NOTIFICATIONS)
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean) 'change to Activity_PermissionResult if non-B4XPages.
Log(Permission & ": " & Result)
Return Result
End Sub
#End If
Sub AddWobbleMenu(wm As WobbleMenu, thisTab As Int)
wm.SetTabTextIcon(1, "Home", Chr(0xF285), Typeface.FONTAWESOME)
wm.SetTabTextIcon(2, "Notifications", Chr(0xF0A2), Typeface.FONTAWESOME)
wm.SetTabTextIcon(3, "Profile", Chr(0xF2C0), Typeface.FONTAWESOME)
wm.SetTabTextIcon(4, "Devices", Chr(0xF1EB), Typeface.FONTAWESOME)
wm.SetTabTextIcon(5, "Request", Chr(0xF05A), Typeface.FONTAWESOME)
wm.SetCurrentTab2(thisTab, False)
End Sub
Sub SetWobbleMenuPosition(wm As WobbleMenu, thisTab As Int)
If wm.GetCurrentTab <> thisTab Then
wm.SetCurrentTab2(thisTab, False)
End If
End Sub
#Region Drawer
#if B4J
Sub imgHamburger_MouseClicked (EventData As MouseEvent)
Drawer.LeftOpen = True
End Sub
#else if B4i
Private Sub B4XPage_MenuClick (Tag As String)
If Tag = "hamburger" Then
Drawer.LeftOpen = Not(Drawer.LeftOpen)
End If
End Sub
#end if
Private Sub B4XPage_CloseRequest As ResumableSub
'home button
If Splash.ActionBarHomeClicked Then
Return False
End If
Dim Obj As Object = xui.Msgbox2Async("Close iS-PROTECTOR Application?", "iS-PROTECTOR", "Yes", "", "No", Null)
Wait For (Obj) Msgbox_Result (Result As Int)
If Result = xui.DialogResponse_Positive Then
Return True
End If
Return False
End Sub
Sub B4XPage_Resize (Width As Int, Height As Int)
End Sub
Private Sub B4XPage_Appear
wait for (Starter.CountNotifications) Complete (NotificationsCounted As Boolean)
wait for (Starter.CountDevices) Complete (DevicesCounted As Boolean)
wait for (Starter.WeHaveAUser) Complete (UserCounted As Boolean)
wait for (LoadDevices) Complete (Done As Boolean)
B4XPages.SetTitle(Me, "PROTECTOR")
ActivePage = "MainPage"
SetWobbleMenuPosition(WobbleMenu1, 1)
imgSOS.Visible = True
UpdateBadge
UpdateDeviceCount
'user is not registered
If UserCounted = False Then
'user must register
NavigateTo("pg3")
Return
End If
'NavigateTo("MainPage")
WobbleMenu1.SetVisible(True,False)
'clear the grid menu
#if B4A
Sleep(0)
B4XPages.GetManager.ActionBar.RunMethod("setDisplayHomeAsUpEnabled", Array(True))
Dim bd As BitmapDrawable
bd.Initialize(HamburgerIcon)
B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(bd))
#End If
End Sub
'
Private Sub B4XPage_Disappear
'#if B4A
B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(0))
'#end if
End Sub
Sub B4XPage_Foreground
End Sub
Sub B4XPage_Background
End Sub
Sub NavigateTo(Value As String)
Select Case Value
Case "pg2", "pg3", "pg4", "pg5", "MainPage"
B4XPages.ShowPage(Value)
End Select
End Sub
'when a notification Is clicked
Sub ActivityResume
'LogColor("MainPage.Activity_Resume...", xui.Color_Magenta)
' StartService(FirebaseMessaging)
Wait For (Starter.WeHaveAUser) Complete (Done As Boolean)
Dim in As Intent = B4XPages.GetNativeParent(Me).GetStartingIntent
If in.IsInitialized And in <> LastIntent Then
LastIntent = in
If in.HasExtra("Notification_Tag") Then
End If
End If
End Sub
'
Sub PlaySound
MP.load(File.DirAssets, "alarm-15.mp3")
MP.Looping = False
MP.play
End Sub
Sub LoadDevices As ResumableSub
lstDevicesHome.Clear
Private dbUser As B4XDBUtils
dbUser.Initialize(modAVP.SQLiteDB, "devices", "id", "id")
dbUser.Execute("SELECT * FROM devices ORDER BY msgdate DESC")
dbUser.FromJSON
Dim notifTot As Int = dbUser.result.Size
modAVP.DeviceNumber = notifTot
modAVP.Devices.Initialize
For Each devicem As Map In dbUser.result
Dim sdeviceid As String = devicem.Get("deviceid")
Dim sdevicename As String = devicem.GetDefault("devicename", "")
modAVP.Devices.Put(sdeviceid, sdevicename)
lstDevicesHome.Add(CreateDevice(lstDevicesHome.AsView.Width, 90dip, devicem), sdeviceid)
Next
Return True
End Sub
' Create a Panel for each device showing latest device status
Private Sub CreateDevice(width As Int, height As Int, notif As Map) As Panel
Dim sdeviceid As String = notif.Get("deviceid")
Dim smsgtypedesc As String = notif.GetDefault("msgtypedesc","New")
Dim svoltage As String = notif.GetDefault("voltage","")
Dim smsgdate As String = notif.GetDefault("msgdate", "")
Dim smsgtype As String = notif.GetDefault("msgtype", "")
'get the device
Dim devicename As String = modAVP.Devices.GetDefault(sdeviceid, "")
Dim stitle As String = $"${devicename}: ${smsgtypedesc}"$
' The following added due to PIR voltage read error
If (smsgtype = "11" Or smsgtype = "12") Then
If svoltage = "1.5" Then
svoltage = "3.0"
End If
End If
'
Dim p As B4XView = xui.CreatePanel("")
p.Height = height
p.Width = width
p.Top = 0
p.Left = 0
p.LoadLayout("homedevices")
'set the elements
msgdatedevicesHome.Text = modAVP.NiceDate(smsgdate)
msgdevicesHome.Text = stitle
Select Case smsgtypedesc
Case "New"
imgdeviceicon.Text = Chr(0xF1EB)
imgdeviceicon.Visible = True
msgdoor.Visible = False
msgvolvage.Visible = False
Case Else
imgdeviceicon.Visible = False
msgvolvage.Visible = True
msgdoor.Visible = True
msgdatedevicesHome.Visible = True
msgdatedevicesHome.Text = modAVP.NiceDate(smsgdate)
Dim vlt As Double = NumberFormat2(svoltage,0,2,2,False)
If vlt >= 2.9 Then msgvolvage.Bitmap = xui.LoadBitmap(File.DirAssets, "darkgreen.png")
If vlt >= 2.6 And vlt <= 2.8 Then msgvolvage.Bitmap = xui.LoadBitmap(File.DirAssets, "green.png")
If vlt >= 2.3 And vlt <= 2.5 Then msgvolvage.Bitmap = xui.LoadBitmap(File.DirAssets, "yellow.png")
If vlt >= 2.0 And vlt <= 2.2 Then msgvolvage.Bitmap = xui.LoadBitmap(File.DirAssets, "amber.png")
If vlt <= 1.9 Then msgvolvage.Bitmap = xui.LoadBitmap(File.DirAssets, "red.png")
'
Select Case smsgtype
Case "00"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "closed.png")
Case "01"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "open.png")
Case "03"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "tilt.png")
Case "04"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "closed.png")
Case "05"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "open.png")
Case "06"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "Vib-Red.png")
Case "07"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "Vib-Blue.png")
Case "09"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "SOS.png")
Case "10"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "eyeblue.png")
Case "11"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "eyegreen.png")
Case "12"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "eyered.png")
Case "20"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "boltgreen.png")
Case "21"
msgdoor.Bitmap = xui.LoadBitmap(File.DirAssets, "boltred.png")
End Select
End Select
p.Tag = notif
Return p
End Sub
Sub GetMessageDescription(xtype As String) As String
Select Case xtype
Case "00"
Return "Closed"
Case "01"
Return "Open"
Case "03"
Return "Under Attack"
Case "04"
Return "Closed"
Case "05"
Return "Open"
Case "06"
Return "Start"
Case "07"
Return "Stop"
Case "09"
Return "SOS"
Case "10"
Return "Disarmed"
Case "11"
Return "Armed"
Case "12"
Return "Motion Detected"
Case "20"
Return "Power ON"
Case "21"
Return "Power OFF"
Case Else
Return "Unknown"
End Select
End Sub
Private Sub lstDevicesHome_ItemClick (Index As Int, Value As Object)
If MP.IsPlaying Then
' CallSub(FirebaseMessaging, "StopPlaying")
MP.Stop
End If
End Sub
Private Sub imgSOS_LongClick
'we long click the sos button
Dim profile As Map = modAVP.MyProfile
Dim semergency As String = profile.GetDefault("emergency","")
If semergency <> "" Then
'
Dim RP As RuntimePermissions
If RP.Check(RP.PERMISSION_CALL_PHONE) = False Then 'if no existing phone call permission
RP.CheckAndRequest(RP.PERMISSION_CALL_PHONE) 'prompt for permission
Wait For Activity_PermissionResult (Permission As String, AskResult As Boolean)
If AskResult = False Then 'user did not give permission
MsgboxAsync("Phone Call Permissions should be activated!", "Phone Error")
Return
End If
End If
'make a phone call
Dim PC As PhoneCalls
StartActivity(PC.Call(semergency))
Else
ToastMessage.Show("The emergency number is required!")
End If
' End If
End Sub
Stepping through the code with debug, it seems the error is related to:
B4X:
B4XPages.GetManager.ActionBar.RunMethod("setDisplayHomeAsUpEnabled", Array(True))
B4X:
Dim bd As BitmapDrawable
bd.Initialize(HamburgerIcon)
B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(bd))
Last edited: