Android Question Immersive mode, java error

Sergio83

Active Member
Licensed User
Longtime User
Good morning all of you,

I'm porting some of my apps to a higher version of Android.

These apps are ran in immersive mode, they worked well with Android 8.1, but failed with Android 11.

B4A version is 11.80, sdk version is 30.

Below is the beginning of error log:

Error log:
Logger connecté à :  PRITOM P7
--------- beginning of main
debug hasfocus:true
main$ResumableSub_Activity_WindowFocusChangedresume (java line: 515)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Exception anywheresoftware.b4a.BA.getLastException()' on a null object reference
    at anywheresoftware.b4a.keywords.Common.LastException(Common.java:821)
    at serisyvis.domcons.main$ResumableSub_Activity_WindowFocusChanged.resume(main.java:515)
    at serisyvis.domcons.main._activity_windowfocuschanged(main.java:442)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at serisyvis.domcons.main.onWindowFocusChanged(main.java:186)
    at com.android.internal.policy.DecorView.onWindowFocusChanged(DecorView.java:1691)
    at android.view.View.dispatchWindowFocusChanged(View.java:14582)
    at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1502)
    at android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:3283)
    at android.view.ViewRootImpl.access$1100(ViewRootImpl.java:191)
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:5079)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7695)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
--------- beginning of crash
mainonWindowFocusChanged (java line: 186)
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Exception anywheresoftware.b4a.BA.getLastException()' on a null object reference
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:250)
    at serisyvis.domcons.main.onWindowFocusChanged(main.java:186)
    at com.android.internal.policy.DecorView.onWindowFocusChanged(DecorView.java:1691)
    at android.view.View.dispatchWindowFocusChanged(View.java:14582)
    at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1502)
    at android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:3283)
    at android.view.ViewRootImpl.access$1100(ViewRootImpl.java:191)
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:5079)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7695)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Exception anywheresoftware.b4a.BA.getLastException()' on a null object reference
    at anywheresoftware.b4a.keywords.Common.LastException(Common.java:821)

The code involved is below:

Activity create code:
Sub Activity_Create(FirstTime As Boolean)
    Dim p As Phone
    
    If FirstTime Then
        ' Stop service STARTATBOOT
        StopService(STARTATBOOT)
    End If
    
    If p.SdkVersion >= 28 Then
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        ctxt.RunMethodJO("getWindow", Null).RunMethodJO("getAttributes", Null).SetField("layoutInDisplayCutoutMode", 1)
    End If
    
    GetRealSize
    Activity_WindowFocusChanged(True)
    Dim lv As LayoutValues = GetRealSize
    Dim jo As JavaObject = Activity
    jo.RunMethod("setBottom", Array(lv.Height))
    jo.RunMethod("setRight", Array(lv.Width))
    Activity.Height = lv.Height
    Activity.Width = lv.Width
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
    
End Sub

Code of sub's is below:

Code of sub's involved:
'===== Routines immersive mode
Sub GetRealSize As LayoutValues
    Dim lv As LayoutValues
    Dim ctxt As JavaObject
    
    ctxt.InitializeContext
    Dim WindowManager As JavaObject = ctxt.RunMethodJO("getSystemService", Array("window"))
    Dim display As JavaObject = WindowManager.RunMethod("getDefaultDisplay", Null)
    Dim point As JavaObject
    point.InitializeNewInstance("android.graphics.Point", Null)
    display.RunMethod("getRealSize", Array(point))
    lv.Width = point.GetField("x")
    lv.Height = point.GetField("y")
    Log(lv.Height)
    lv.Scale = 100dip / 100
    Return lv
    
End Sub

Sub Activity_WindowFocusChanged(HasFocus As Boolean)
Log("debug hasfocus:"&HasFocus)   
    If HasFocus Then
        Try
            Dim jo As JavaObject = Activity
            Sleep(300)
            jo.RunMethod("setSystemUiVisibility", Array As Object(5894)) '3846 - non-sticky
        Catch
            Log(LastException) 'This can cause another error
        End Try 'ignore
        
    End If
    
End Sub

Except an error from me, it is the code which is recommanded by Erel to run an app in immersive mode.

Where am I wrong?

Many thanks by advance for your help
 

Sergio83

Active Member
Licensed User
Longtime User
Hello guys

I pushed my investigations further more.

First I upgrade to B4A 12.80, then same result, with the same device.

I tried to use another Android device (version 13) and the issue I was facing has not occured again. So I think that my problem was due to the device itshelf, not to the code or the Android version.

So this thread has to be closed!
 
Upvote 0
Top