Android Question acivity self stops.

aeropic

Active Member
Licensed User
Longtime User
Hi All,

After 15 days in the fabulous world of B4A, I'm developping a homewidget that should cycle the screen ON/OFF just before the lockscreen time out triggers.
I do this using:
- a Main activity in charge of UI and preferences management
- a widget service (based upon homewidget tuto) doing quite nothing but managing its icons and starting another service
- the other service rechedulled (startserviceat) based on events such as ScreenON or ScreenOFF ...

It used to work fine except sometimes when uploading a new version of the application it refuses to restart.
I get the msgbox (in french) ["Cycle Screen s'est arrêté" OK] which could be translated in Cycle screen self stopped or stopped by its own.

Under the debugger I see a Nullpointer exception

Installing file.
PackageAdded: package:anywheresoftware.b4a.samples.homewidgets
maininitializeProcessGlobals (java line: 475)
java.lang.RuntimeException: java.lang.NullPointerException
at anywheresoftware.b4a.samples.homewidgets.main.initializeProcessGlobals(main.java:475)
at anywheresoftware.b4a.samples.homewidgets.main.afterFirstLayout(main.java:85)
at anywheresoftware.b4a.samples.homewidgets.main.access$100(main.java:16)
at anywheresoftware.b4a.samples.homewidgets.main$WaitForLayout.run(main.java:74)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at anywheresoftware.b4a.keywords.Common.getComponentIntent(Common.java:735)
at anywheresoftware.b4a.objects.NotificationWrapper.SetInfo2(NotificationWrapper.java:141)
at anywheresoftware.b4a.objects.NotificationWrapper.SetInfo(NotificationWrapper.java:121)
at anywheresoftware.b4a.samples.homewidgets.widgetservice._process_globals(widgetservice.java:169)
at anywheresoftware.b4a.samples.homewidgets.main.initializeProcessGlobals(main.java:470)
... 12 more

My main does not include any ProcessGlobal variables.
I wonder if the problem is coming from a bad understanding of what does the StateManager ?

When in this state, it happens even that B4A_bridge does not succed to update the application.
I tried removing, the data application, stopping the application from APP mangement, sometimes with success but often without...

In other words, I'm stalled :(

Any help would be appreciated
Thanks
Alain

Here is my Main:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    Dim p As Phone
    Dim lock_TO As Int
    Dim SeekBar_sect As SeekBar
    Dim SeekBar_Batt As SeekBar
    Dim Label_Sect As Label
    Dim Label_Batt As Label
    Dim Bt_wifi As Button
    Dim mywifi As ABWifi
    Dim Ed_wifi As EditText
    Dim Label_wifi As Label
    Dim SeekBar_wifi As SeekBar
    Dim Vibrate As PhoneVibrate ' For phone vibration
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        ToastMessageShow("Go to the home screen and add the Screen Cycle  Widget.", False)
    End If
    
        Activity.LoadLayout("ihm_radio")
       
    lock_TO = 5*60 - p.GetSettings("screen_off_timeout")/1000
    SeekBar_sect.Max = 300
    SeekBar_wifi.Max = 800
    SeekBar_Batt.Max = 60
    Try
        If StateManager.RestoreState(Activity, "Main", 0) = False Then
        ToastMessageShow("terstore false.", False)
            'set the default values
            StateManager.SetSetting("counter_secteur", 100)
            StateManager.SetSetting("counter_Batt", 5)
            StateManager.SetSetting("counter_wifi", 100)
            SeekBar_sect.Value =  300
            SeekBar_wifi.Value =  300
            SeekBar_Batt.Value =  15
            Label_Sect.Text = "secteur : " & 300 & " min"
            Label_Batt.Text = "batterie : " & 15 & " min"
            Label_wifi.Text = "wifi : " & 300 & " min"
            Ed_wifi.Text = "---- click here ---->"
            StateManager.SetSetting ("my_wifi", "---")
            StateManager.SaveSettings
        End If
    Catch
        Vibrate.Vibrate(100)
    End Try

    'Activity.Finish
End Sub

Sub Activity_Resume
Log("tttt")
'ToastMessageShow("resume.", False)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
  ' If UserClosed Then
  '    StateManager.ResetState("Main")
  '  Else
        StateManager.SaveState(Activity, "Main")
  '  End If
    StateManager.SaveSettings
   
End Sub


Sub SeekBar_sect_ValueChanged (Value As Int, UserChanged As Boolean)
    Label_Sect.Text = "secteur : " & Value & " min"
    lock_TO = 5*60 - p.GetSettings("screen_off_timeout")/1000
    StateManager.SetSetting("counter_secteur", Value*60 / lock_TO)
    StateManager.SaveSettings
End Sub
Sub SeekBar_Batt_ValueChanged (Value As Int, UserChanged As Boolean)
    Label_Batt.Text = "Batterie : " & Value & " min"
    lock_TO = 5*60 - p.GetSettings("screen_off_timeout")/1000
    StateManager.SetSetting("counter_secteur", Value*60 / lock_TO)
    StateManager.SaveSettings
End Sub
Sub Bt_wifi_Click
    Dim r As Boolean
      Dim s As String
    r = mywifi.ABLoadWifi()
    If r = True Then
        s = mywifi.ABGetCurrentWifiInfo().SSID
        If s = Null Then s = "no wifi"
        Ed_wifi.Text = s
    Else
      Ed_wifi.Text = "click to add friend wifi ! ====>"
    End If
    StateManager.SetSetting("my_wifi", Ed_wifi.text)
    StateManager.SaveSettings
End Sub

Sub Ed_wifi_TextChanged (Old As String, New As String)
    StateManager.SetSetting("my_wifi", Ed_wifi.text)
    StateManager.SaveSettings
End Sub
Sub SeekBar_wifi_ValueChanged (Value As Int, UserChanged As Boolean)
    Label_wifi.Text = "wifi : " & Value & " min"
    lock_TO = 5*60 - p.GetSettings("screen_off_timeout")/1000
    StateManager.SetSetting("counter_wifi", Value*60 / lock_TO)
    StateManager.SaveSettings
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The problem is in Process_Globals of the widget service:

Caused by: java.lang.NullPointerException
at anywheresoftware.b4a.keywords.Common.getComponentIntent(Common.java:735)
at anywheresoftware.b4a.objects.NotificationWrapper.SetInfo2(NotificationWrapper.java:141)
at anywheresoftware.b4a.objects.NotificationWrapper.SetInfo(NotificationWrapper.java:121)
at anywheresoftware.b4a.samples.homewidgets.widgetservice._process_globals(widgetservice.java:169)
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
thank you Erel.
Your answer gave me the indication to search in the widget...

The widget service is more or less derived from the tutorial.
It includes one button whose icon can change from "locked" to "unlocked".
(edited)
I have better understood the way my widget bugs. It is linked to the usage of a notification.
When the user clicks on the icon, a notification is sent :
n.Notify(1)
this notification is defined to be linked to main :
n.SetInfo("Lock Screen inhibé !", "", "Main")

It is cancelled by a click on the widget icon: n.Cancel(1)

As long as nobody clicks on the notification, main remains OK. The activity is launched 100% of the time.

when the notification is clicked, main is shown everytime
When the notification is cancelled, by a click on the widget icon, then displayed again, by another click on the widget, if the user clicks on the notification: main fails to display...

I'm stalled again :(

here is my widget code.
B4X:
#Region Module Attributes
   #StartAtBoot: False
#End Region

'Service module
Sub Process_Globals
   Dim rv As RemoteViews
   Dim imageFiles() As String
   imageFiles = Array As String("icon_ferme.gif", "icon.gif")
   Dim currentImage As Int

   Dim n As Notification
     
   
End Sub
Sub Service_Create
   'Set the widget to update every 600 minutes.
   rv = ConfigureHomeWidget("L1", "rv", 600, "Screen Cycle",True)
   n.Initialize
     n.Icon = "icon"
     n.Sound = False
     n.Vibrate = False
     n.OnGoingEvent = True
     n.AutoCancel = False
     n.SetInfo("Lock Screen inhibé !", "", "Main") 'Change Main to "" if this code is in the main module.
   
End Sub

Sub Service_Start (StartingIntent As Intent)
   If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub

Sub rv_RequestUpdate
   rv.UpdateWidget
End Sub

Sub rv_Disabled
   StopService("")
   Main.ForceClose = True
End Sub

Sub Service_Destroy

   n.Cancel(1)
   Main.ForceClose = True
End Sub

Sub WidgetUpdate
   
   n.Cancel(1)
   rv.SetImage("ImageView1", LoadBitmap(File.DirAssets, "icon_ferme.gif"))
   rv.UpdateWidget
   
End Sub

Sub ImageView1_Click
   currentImage = (currentImage + 1) Mod imageFiles.Length
   rv.SetImage("ImageView1", LoadBitmap(File.DirAssets, imageFiles(currentImage)))
   rv.UpdateWidget
   
   If currentImage = 1 Then
     StartService(test_service)
     ToastMessageShow("Lock Screen inhibé" , False)
     n.Notify(1)
     Log("notification")
   Else
     n.Cancel(1)
     Log("notification cancelled")
     CancelScheduledService (test_service)
     StopService(test_service)
     ToastMessageShow("Lock Screen actif", False)
     Main.ForceClose = True
     StopService("")
     
   End If
'   ToastMessageShow(test_service.counter_Batt, False)
End Sub
 
Last edited:
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
You're right Erel. I've found my mistake, when tying to fix it I introduced several modifications at a time:
1- I moved this piece of code from widget process global to service create :
B4X:
n.Initialize
    n.Icon = "icon"
    n.Sound = False
    n.Vibrate = False
    n.OnGoingEvent = True
    n.AutoCancel = False
    n.SetInfo("Lock Screen inhibé !", "", "Main") 'Change Main to "" if this code is in the main module.
2- I added Main.forceClose = true
and this was the reason of the strange behavour I got on my previous post !

Could be the first modif (1-) the reason of the null pointer exception I got previously ? It seems, I do not get this error anymore ...
 
Upvote 0
Top