Android Question Crash upon Activity.Finish

DawningTruth

Active Member
Licensed User
In my activity, there is a scenario where the user can click the back button before the activity is loaded. This back button click needs to be handled.

What I basically do is:

B4X:
// Back Button Handling Code

// Detect Premature Exit
If prematureExit = true then

   Activity.Finish

end if
When I run the code, I get the Java Crash below. Presumably, because the elements of the activity are only half loaded:

Error occurred on line: 2374 (WB)
java.lang.RuntimeException: Object should first be initialized (B4XView).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.B4XViewWrapper.asLabelWrapper(B4XViewWrapper.java:191)
at anywheresoftware.b4a.objects.B4XViewWrapper.getTextColor(B4XViewWrapper.java:219)
at b4a.example.WB$ResumableSub_ExecuteBackRule.resume(WB.java:1595)
at b4a.example.WB._executebackrule(WB.java:1528)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.WB$HandleKeyDelayed.runDirectly(WB.java:232)
at b4a.example.WB$HandleKeyDelayed.run(WB.java:229)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

** Activity (WB) Pause, UserClosed = true **

Any suggestions on how to prevent this crash? Or how to better handle the exit so that the crash does not occur.
 

JohnC

Well-Known Member
Licensed User
What is the code in your Activity_Pause?

It looks like you have code in the Activity_Resume sub that is trying to do something with a B4AView, but that B4AView has not been initialized yet, so that is why it's trowing an error.

If you click on the first line of the error:

B4X:
Error occurred on line: 2374 (WB)
Does it point to the line of code causing the error?
 
Last edited:

DawningTruth

Active Member
Licensed User
What is the code in your Activity_Pause?

It looks like you have code in the Activity_Resume sub that is trying to do something with a B4AView, but that B4AView has not been initialized yet, so that is why it's trowing an error.

If you click on the first line of the error:

B4X:
Error occurred on line: 2374 (WB)
Does it point to the line of code causing the error?
Thx John,

I believe I have found the error. Will have to just check. There is some code in Pause which is used for transitioning from Portrait to Landscape which also has the dependency. I suspect that is the culprit.
 

JohnC

Well-Known Member
Licensed User
I haven't tested this, but this might be a quick fix...

You'll notice that that Activity_Pause routine has a parameter called "UserClosed as Boolean":
B4X:
Sub Activity_Pause (UserClosed As Boolean)
And a fix might be to do this:

B4X:
Sub Activity_Pause (UserClosed As Boolean)

    If UserClosed = False then
       ....
       (your code here)
       ....
    End if

End Sub
Then none of your code will run if the user closed the activity, but if the user simply switch orientation, then the code will run.
 
Last edited:

DawningTruth

Active Member
Licensed User
Thx John,

Turns out as the Activity.Finish was called from a Resumable Sub (It warns the user first and asks them if they want to cancel.), it was crashing as the resumable sub for some strange reason wasn't finished. I added a Return Null to the resumable sub and it is now working.
 
Top