Android Question What am I missing? Error Getting buttons from input Dialog

MrKim

Well-Known Member
Licensed User
Longtime User
The code below works fine in B4j but is failing in B4A. The error, as you can see below is in B4XDialog. I tried rearranging the order in which I get the three buttons but it doesn't matter. It ALWAYS works fine getting the FIRST button and fails on the second one. Am I missing something obvious again?

B4X:
Sub InputDlg(Title As String, TitleTextSize As Int, DefText As String) As ResumableSub
    Dim input As B4XInputTemplate
    input.Initialize
    input.TextField1.TextColor = xui.Color_Black
    input.Text = DefText
    input.TextField1.EditTextHint = "Names are case sensitive"
    input.lblTitle.TextColor = xui.Color_Black
    If TitleTextSize > 0 Then input.lblTitle.TextSize = TitleTextSize
    input.SetBorderColor(xui.Color_Black, xui.Color_Red)
    Dim Pnl As B4XView = xui.CreatePanel("")
    Pnl.Color = xui.Color_White
    input.TextField1.Parent.AddView(Pnl, input.TextField1.Left, input.TextField1.Top, input.TextField1.Width, input.TextField1.Height)
    Pnl.SendToBack
'    input.TextField1.SetColorAndBorder(XUI.Color_ARGB(255, 0, 0,0), 1dip, XUI.Color_Black, 0)
    Dialog.BlurBackground = False
    Dialog.BackgroundColor = 0xFFF1F1F1
    Dialog.ButtonsColor = 0xFFB3B3B3  'you CAN set colors directly from the color picker
    Dialog.ButtonsTextColor = xui.Color_Black
    Dialog.OverlayColor = xui.Color_ARGB(95, 0, 0, 0)  'this sets the overall background
    Dialog.ButtonsOrder = Array As Int(xui.DialogResponse_Negative, xui.DialogResponse_Cancel, xui.DialogResponse_Positive)
    input.lblTitle.Text = Title
    Dim Dlg As Object = Dialog.ShowTemplate(input, "OK", "", "CANCEL")
'    BorderForBtns(Dialog.GetButton(xui.DialogResponse_Cancel))
'    BorderForBtns(Dialog.GetButton(xui.DialogResponse_Positive))
'    BorderForBtns(Dialog.GetButton(xui.DialogResponse_Negative))
    Dim X As Int, Btn As B4XView
    For x = -3 To -1
        Btn = Dialog.GetButton(X)
'        put a border around the buttons
        If Btn.IsInitialized Then
            Dim Pnl As B4XView = xui.CreatePanel("")
            Pnl.SetColorAndBorder(xui.Color_Black, 2dip, xui.Color_Black, 5dip)
            Btn.Parent.AddView(Pnl, Btn.Left - 2dip, Btn.Top - 2dip, Btn.Width + 2dip, Btn.Height + 2dip)
            Pnl.SendToBack
'            Btn.RequestFocus  'doesn't work Set the focus to the button so the hint text is visible
        End If
    Next
    Wait For (Dlg) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Return input.Text
    End If
    Return ""
End Sub

Error::
** Activity (main) Resume **
Error occurred on line: 217 (B4XDialog)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
    at com.SuperlativeSoftware.LearALanguage.b4xdialog._getbutton(b4xdialog.java:216)
    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:157)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at com.SuperlativeSoftware.LearALanguage.recordpg._inputdlg(recordpg.java:492)
    at com.SuperlativeSoftware.LearALanguage.recordpg$ResumableSub_QuickSaveBtn_Click.resume(recordpg.java:992)
    at com.SuperlativeSoftware.LearALanguage.recordpg._quicksavebtn_click(recordpg.java:775)
    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:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7729)
    at android.view.View.performClickInternal(View.java:7706)
    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
    at android.view.View$PerformClick.run(View.java:30484)
    at android.os.Handler.handleCallback(Handler.java:959)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loopOnce(Looper.java:232)
    at android.os.Looper.loop(Looper.java:317)
    at android.app.ActivityThread.main(ActivityThread.java:8501)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)

Thanks for any help.
 

MrKim

Well-Known Member
Licensed User
Longtime User
Change the If line to:
B4X:
If Btn <> Null And Btn.IsInitialized Then
The error is actually occurring inside the B4XDialog
B4X:
Error occurred on line: 217 (B4XDialog)
on this line:
B4X:
Btn = Dialog.GetButton(X)
so I never actually get a button to test.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Is this the result you would like to do ?

微信图片_20240618181715.png
 

Attachments

  • Project.zip
    15.9 KB · Views: 4
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Is this the result you would like to do ?
Well, yes, that solves the immediate problem but if I modify the routine to use different buttons at different times then I need be able to test if each of the 3 buttons exist. Another way to handle that would be to set variable for each button text (Which I may do later by passing the button text) and if it is a zero length string leave it alone. But Getbutton shouldn't (I think?) fail inside the lib.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Well, yes, that solves the immediate problem but if I modify the routine to use different buttons at different times then I need be able to test if each of the 3 buttons exist. Another way to handle that would be to set variable for each button text (Which I may do later by passing the button text) and if it is a zero length string leave it alone. But Getbutton shouldn't (I think?) fail inside the lib.
1. The first button(Yes) is mandatory in ShowTemplate, you should use try catch to check if others are existed .
2. If you just would like to set button border, you don't need to add a panel and directly do Btn.SetColorAndBorder.
B4X:
    Dim X As Int, Btn As B4XView
    For x = -3 To -1
        Try
            Btn = Dialog.GetButton(X)
        Catch
            Continue
        End Try
 
        '        put a border around the buttons
        If  Btn.IsInitialized Then
            Btn.SetColorAndBorder(Btn.Color, 5dip, xui.Color_black, 5dip)

        End If
    Next
 
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
1. The first button(Yes) is mandatory in ShowTemplate, you should use try catch to check if others are existed .
2. If you just would like to set button border, you don't need to add a panel and directly do Btn.SetColorAndBorder.
B4X:
    Dim X As Int, Btn As B4XView
    For x = -3 To -1
        Try
            Btn = Dialog.GetButton(X)
        Catch
            Continue
        End Try
 
        '        put a border around the buttons
        If  Btn.IsInitialized Then
            Btn.SetColorAndBorder(Btn.Color, 5dip, xui.Color_black, 5dip)

        End If
    Next
1. You need to read my post. It is failing in the DIAOLOG and blowing completely out of the program.
2. SetColorAndBorder I know that, but that makes the buttons too small for me. The simple solution was to just add a panel.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I have read your post. as I said in #8, the first button(Yes) can't be passed "", you have to give a value to sub showtemplate.
if you would like to pass "" to the first button(yes), you need to modify xui view library. else it will crash.

B4X:
Dim Dlg As Object = Dialog.ShowTemplate(input, "XXX", "", "")
 

Attachments

  • Project.zip
    15.9 KB · Views: 4
Last edited:
Upvote 0
Top