Android Question B4X error using B4XPage_CloseRequest

amer bashar

Member
Licensed User
Hi,
for using the android back button I used this code in the B4XMainPage:

B4X:
private Sub B4XPage_CloseRequest As ResumableSub
    Dim sf As Object = xui.Msgbox2Async("Close?", "Title", "Yes", "Cancel", "No", Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Return True
    End If
End Sub

and this default code in Main :
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub

This error occured :
Error occurred on line: 7208 (B4XPagesDelegator)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.keywords.Common.CallSubDebug(Common.java:1050)
at sama.sport.b4xpagesmanager$ResumableSub_HandleCloseRequest.resume(b4xpagesmanager.java:843)
at sama.sport.b4xpagesmanager._handlecloserequest(b4xpagesmanager.java:810)
at sama.sport.b4xpagesmanager._activity_keypress(b4xpagesmanager.java:565)
at sama.sport.b4xpagesdelegator._activity_keypress(b4xpagesdelegator.java:51)
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 sama.sport.main$HandleKeyDelayed.runDirectly(main.java:231)
at sama.sport.main$HandleKeyDelayed.run(main.java:228)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7266)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:336)
at anywheresoftware.b4a.debug.Debug.CallSubNew(Debug.java:282)
... 21 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
... 22 more
Caused by: java.lang.ClassCastException: anywheresoftware.b4a.keywords.Common$ResumableSubWrapper cannot be cast to java.lang.String
at sama.sport.b4xmainpage._b4xpage_closerequest(b4xmainpage.java:5824)
... 24 more

It is unbelievable I have already wrote 9000 lines code for his app and published it to appstore and still cant use the android back button ??
 

agraham

Expert
Licensed User
Longtime User
Your code in B4XPage_CloseRequest is what I have used in B4XMainPage in my B4XPages test app. I've just tried it in Relase and both legacy and normal debug modes with no problem.
B4X:
Private Sub B4XPage_CloseRequest As ResumableSub
    Log("MainPage CloseRequest")
    ' Close the Drawer and don't exit if it's open
    If Drawer.LeftOpen Then
        CloseDrawerAndReset
        Return False
    End If
    ' The usual make sure dialog
    Dim sf As Object = xui.Msgbox2Async("Close?", "B4XPagesTest", "Yes", "Cancel", "No", Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    Log(Result)
    If Result = xui.DialogResponse_Positive Then
        Return True
    End If
    Return False
End Sub
I'm a bit puzzled as to how you can get a different result unless things are not as you posted forB4XPage_CloseRequest. I would run it in release mode and if the error occurs look in the b4xmainpage.java file in the project Object/src/... folder tree around the line number given by b4xmainpage.java: in the error line
Caused by: java.lang.ClassCastException: anywheresoftware.b4a.keywords.Common$ResumableSubWrapper cannot be cast to java.lang.String
at sama.sport.b4xmainpage._b4xpage_closerequest(b4xmainpage.java:5824)

for the Java code for the _b4xpage_closerequest and see the Java code for what is happening.
 
Upvote 0

amer bashar

Member
Licensed User
Thank you very much,
I tried Release mode and it crashes at the same situation
I reviewed the java file you mentioned around the line 5824 but nothing is related to back button in that code, unfortunately I didn`t understand anything in java

case 154:
//do until
this.state = 157;
while (!(_randomplayer[(int) (14)]!=_randomplayer[(int) (13)])) {
this.state = 156;
if (true) break;
}
if (true) break;

case 156:
//C
this.state = 154;
//BA.debugLineNum = 8517;BA.debugLine="randomplayer(14) = Rnd(0,count)";
_randomplayer[(int) (14)] = parent.__c.Rnd((int) (0),_count);
if (true) break;
 
Upvote 0

amer bashar

Member
Licensed User
Sorry for my stupidity but I tried another way :
I called a sub in B4XMainPage that sub runs the code I need for the back button, I called it from Main as following :
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    B4XPages.MainPage.TopBack_Touch(0,0,0)
    'Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub
So it runs perfect and immediately closed the app (its default behavior)
How can I stop it from closing? Thank you for your help
 
Upvote 0
Top