Android Question B4XDialog, Button set to ""

MList

Active Member
Licensed User
Error occurred on line: 214 (B4XDialog)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
at b4a.SondeTransfer.b4xdialog._getbutton(b4xdialog.java:757)
at b4a.SondeTransfer.subs._setdialog(subs.java:465)
at b4a.SondeTransfer.b4xmainpage$ResumableSub_StartSub.resume(b4xmainpage.java:1673)
at b4a.SondeTransfer.b4xmainpage._startsub(b4xmainpage.java:1628)
at b4a.SondeTransfer.b4xmainpage$ResumableSub_btnConnect_Click.resume(b4xmainpage.java:1536)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
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:146)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1717)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8751)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)


In B4J it works correct, it is because ButtonCancel = ""
How can i look which button is set in sub SetDialog ?

Thanks for your help
Marion

Set DialogView:
B4XPages.MainPage.msgText.Visible = True
B4XPages.MainPage.msgTitel.Text = "Bitte Speichername eingeben"
[B]MsgO = B4XPages.MainPage.msgdialog.showcustom(B4XPages.MainPage.DialogView,"OK",Language.Text(Var.v,74) , "")
Subs.SetDialog(B4XPages.MainPage.msgdialog)[/B]
wait for (MsgO) Complete (result As Int)
If result = xui.DialogResponse_Positive Then



Public Sub SetDialog(V As B4XDialog)
ground = True
    v.BackgroundColor = Var.limablue
    V.ButtonsHeight = 10dip

    v.BorderColor = Var.lblColor
    v.ButtonsOrder = Array As Int(xui.DialogResponse_Positive,xui.DialogResponse_Cancel, xui.DialogResponse_Negative)
    v.BorderCornersRadius = 15dip
    v.BorderWidth = 1dip
    #if b4a
      V.BorderWidth = 3dip
    #End If
 
    
    v.TitleBarFont = Var.BigFont
    v.TitleBarColor = xui.Color_Transparent
    v.TitleBarTextColor = Var.lblColor
    
    Dim Abstand As Int
    Dim b1,b2,b3 As B4XView
    
    b1 = v.GetButton(xui.DialogResponse_Positive)
    If b1.IsInitialized = True Then
'        b1.Height = 15dip
'        b1.TextSize = 12
'        Abstand = ((Var.ScreenWidth * 0.4) - (3 * (b1.Width))) / 4
'        b1.TextColor = Var.txtColor
'        b1.Left = Abstand
'        b1.Top =(Var.ScreenHeight * 0.3) - 5dip
'        b1.SetColorAndBorder(Var.limadarkblue,1dip,xui.Color_White,15dip)
        Abstand = ((Var.ScreenWidth) - (3 * (b1.Width))) / 4
        b1.TextSize = 12
        b1.Height = 35dip
        b1.TextColor = Var.txtColor
        b1.Left = Abstand
        b1.SetColorAndBorder(Var.limadarkblue,1dip,xui.Color_White,15dip)

    End If   
    b2 = v.GetButton(xui.DialogResponse_Negative)
    If b2.IsInitialized = True Then
        Abstand = ((Var.ScreenWidth) - (3 * (b2.Width))) / 4
        b2.TextSize = 12
        b2.Height = 35dip
        b2.TextColor = Var.txtColor
        b2.Left = Abstand * 2 + b2.Width
        b2.SetColorAndBorder(Var.limadarkblue,1dip,xui.Color_White,15dip)
    End If
'    #if b4j
    If V.GetButton(xui.DialogResponse_Cancel)  < 0 Then
         Log ("OUT")
    End If
  [B]  b3 = V.GetButton(xui.DialogResponse_Cancel)   -->ERROR LINE[/B]
    If b3.IsInitialized = True Then
        b3.textsize = 12
        b3.height = 35dip
        Abstand = ((Var.ScreenWidth) - (3 * (b3.Width))) / 4
        b3.TextColor = Var.txtColor
        b3.Left = Abstand * 3 + (b3.Width) * 2
'        b3.Top = (Var.ScreenHeight * 0.3) - 5dip
        b3.SetColorAndBorder(Var.limadarkblue,1dip,xui.Color_White,15dip)
    End If
'    #End If
 
Solution
Here's a small example of how you could tackle this problem.
B4X:
private Sub show_notedialog
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, 340dip, 250dip)
    pnl.LoadLayout("notedialog_layout")
    Private year As Int = DateTime.GetYear(DateTime.Now)
    Private month As Int = DateTime.GetMonth(DateTime.Now)
    Private day As Int = DateTime.GetDayOfMonth(DateTime.Now)
    Private today As String = day & "/" & month & "/" & year
    lblnote_title.Text = "Add note for " & today
    #if b4a
        edt1_note.text = "A first B4A note" 
    #Else If b4j
        TextArea1.Text = "A first B4J note" 
    #End If
    Dim btntxt0 As String = "Yes"
    Dim btntxt1 As String = "No"
    Dim btntxt2 As String = ""...

PaulMeuris

Active Member
Licensed User
Here's a small example of how you could tackle this problem.
B4X:
private Sub show_notedialog
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, 340dip, 250dip)
    pnl.LoadLayout("notedialog_layout")
    Private year As Int = DateTime.GetYear(DateTime.Now)
    Private month As Int = DateTime.GetMonth(DateTime.Now)
    Private day As Int = DateTime.GetDayOfMonth(DateTime.Now)
    Private today As String = day & "/" & month & "/" & year
    lblnote_title.Text = "Add note for " & today
    #if b4a
        edt1_note.text = "A first B4A note" 
    #Else If b4j
        TextArea1.Text = "A first B4J note" 
    #End If
    Dim btntxt0 As String = "Yes"
    Dim btntxt1 As String = "No"
    Dim btntxt2 As String = ""
    Dim rs As ResumableSub =  notedialog.ShowCustom(pnl, btntxt0, btntxt1, btntxt2)
    If btntxt0 <> "" Then
        Dim btn0 As B4XView = notedialog.GetButton(xui.dialogResponse_Positive)
        #if b4a
            btn0.TextColor = Colors.Green
        #else if b4j
            btn0.TextColor = xui.Color_Green
        #End If
        Log(btn0.Text)
    End If
    If btntxt1 <> "" Then
        Dim btn1 As B4XView = notedialog.GetButton(xui.dialogResponse_Negative)
        #if b4a
            btn1.TextColor = Colors.Red
        #else if b4j
            btn1.TextColor = xui.Color_Red
        #End If
        Log(btn1.Text)
    End If
    If btntxt2 <> "" Then
        Dim btn2 As B4XView = notedialog.GetButton(xui.dialogResponse_Cancel)
        #if b4a
            btn2.TextColor = Colors.Yellow
        #else if b4j
            btn2.TextColor = xui.Color_Yellow
        #End If
        Log(btn2.Text)
    End If
    Wait For (rs) Complete (Result As Int)
    If Result = xui.dialogResponse_Positive Then
        Log("Positive response")
    End If
    If Result = xui.dialogResponse_Negative Then
        Log("Negative response")
    End If
    If Result = xui.dialogResponse_Cancel Then
        Log("Cancel response")
    End If
End Sub
The B4XDialog creates buttons when needed that is when the argument is not an empty string ("").
By using string variables for the arguments you can easily test if a button is available (<> "") (and you can set the text in any language)
When a button is present then you can set its properties according to the environment (B4A or B4J).
In the attachments you can find 2 versions to test.
Note: this example is a B4XPages project.
P.S. : put the line with the #CustomBuildAction in comment when testing
B4X:
'#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
 

Attachments

  • testenvironment22_B4J.zip
    4.4 KB · Views: 132
  • testenvironment22_B4A.zip
    11.5 KB · Views: 122
Last edited:
Upvote 0
Solution

MList

Active Member
Licensed User
Thanks for the example.
So in my case i had to change the code to:

B4X:
If b3.IsInitialized = True And b3.Text <> "" Then

and now it works perfect .
 
Upvote 0
Top