Android Question Immersive mode causes a crash in debug mode

LucaMs

Expert
Licensed User
Longtime User
I "set" immersive mode for the Main; from here I start another Activity and Activity_WindowFocusChanged causes a crash if I start the app in debug mode (see the test project attached).

Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Error occurred on line: 44 (Main)
java.lang.NullPointerException
at b4a.example.main._activity_windowfocuschanged(main.java:427)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.main.onWindowFocusChanged(main.java:185)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2678)
at android.view.View.dispatchWindowFocusChanged(View.java:8018)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:968)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3289)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)

Tested only on an Android 4.4.2.
 

Attachments

  • ImmveModeTest.zip
    9.9 KB · Views: 306
Last edited:

LucaMs

Expert
Licensed User
Longtime User
In the test project attached to the first post, it seems you can solve by changing Sleep(300) to Sleep(100):
B4X:
Sub Activity_WindowFocusChanged(HasFocus As Boolean)
    If HasFocus Then
        Try
            Dim jo As JavaObject = Activity
            Sleep(100)
            jo.RunMethod("setSystemUiVisibility", Array As Object(5894)) '3846 - non-sticky
        Catch
            Log(LastException)
        End Try
    End If
End Sub

but in other projects this change does not solve.
 
Upvote 0
D

Deleted member 103

Guest
Did you read that from Erel?
he code was updated with a small change.

The Log(LastException) line was commented. The Activity_WindowFocusChanged event can fire after the activity was paused and this line can cause an untrapped exception to be raised.

I tried it with the tip of Erel and it works. :)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Sometimes the error happens when I write nothing in the Catch part; so I write a Sleep there but... sometimes Sleep(100) is not enough, I have to increase the time, sometimes it is the opposite, I have to decrease this value.

Going by trial:
sometimes it can be solved by "cleaning" the project (CTRL+P), other times stopping the project using the device settings.


Can you post the logs after you commented the relevant line?
** Activity (main) Pause, UserClosed = false **
Error occurred on line: 393 (Main)
java.lang.NullPointerException
at mbnt.MyProjName.it.main._activity_windowfocuschanged(main.java:695)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at mbnt.MyProjName.it.main.onWindowFocusChanged(main.java:185)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2678)
at android.view.View.dispatchWindowFocusChanged(View.java:8018)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:968)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3289)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Well, MAYBE I found something "useful" (for Erel :)).

Catch block empty. Start B4A, open the project, run and you get the error.
Change the Sleep command (it does not matter how many ms) inside the Activity_FocusChanged event routine and you no more get the error (in the same "b4a session").

Save the project changed, open and start it again... and again you get the error.
 
Upvote 0
Top