Android Question SOLVED - Problem with Custom Dialog

JMB

Active Member
Licensed User
Longtime User
Hi,

I am using the example code shown in Dialogs library 2017 - Custom dialogs and async methods.

I copied the example code using Dialogs 4.01 and the layout file from the example into my own code, and I get an initialisation problem which doesn't appear in the example.

I changed some of the names of the views and this is where the error occurs. It appears that DialogSSID and DialogPassword are not initialised.


B4X:
Sub CheckAllFieldsValid
    Dim valid As Boolean = False
    If DialogSSID.Text.Length > 0 Then
        If DialogPassword.Text.Length > 0 Then
            valid = True
            WifiDialog.GetButton(DialogResponse.POSITIVE).Enabled = valid
        End If
    End If
End Sub

This is a modification of the original sub, done in order to debug. It could obviously be neater and shorter, but I was in debug mode

It fails on the DialogSSID.Text.Length check with:

B4X:
java.lang.RuntimeException: Object should first be initialized (FloatLabeledEditText).

If I comment out the contents of that sub, the dialog shows, but obviously I can't get out of it except by using cancel.

Any help would be much appreciated.

JMB
 

JMB

Active Member
Licensed User
Longtime User
Hi Manfred,

Thank you for the amazingly fast response.

I wondered about that. What I did was copy the layout file from the original example, delete the views there, then recreated my own views and made sure I generated them and the "text_changed" events.

However, I suspect that you may be right and that copying things over hasn't quite worked, so I will try creating a new dialog and see how that works out with entirely new views.

JMB
 
Upvote 0

JMB

Active Member
Licensed User
Longtime User
Hmm..

Didn't work. I created an entirely new layout file, added two FloatLabeledEditText views to it, generated all the members and events for those two views, saved it as "newwifidialog" then modified my code and I am still getting the same error.

Here's the code:

In Globals, I've got:

B4X:
Private WifiDialog As CustomLayoutDialog
Private FloatLabeledEditTextSSID As FloatLabeledEditText
Private FloatLabeledEditTextPassword As FloatLabeledEditText

Then my subs are:

B4X:
Sub btnGetWifiData_Click
    Dim sf As Object = WifiDialog.ShowAsync("Enter Wifi Details", "Ok", "Cancel", "", LoadBitmap(File.DirAssets, "form.png"), True)
   
    WifiDialog.SetSize(100%x, 400dip)
   
    Wait For (sf) Dialog_Ready(pnl As Panel)
   
    pnl.LoadLayout("newwifidialog")
    '0x00002000 = TYPE_TEXT_FLAG_CAP_WORDS (capitalize first character of each word)
    FloatLabeledEditTextSSID.EditText.InputType = Bit.Or(0x00002000, FloatLabeledEditTextSSID.EditText.InputType)
    FloatLabeledEditTextPassword.EditText.InputType = Bit.Or(0x00002000, FloatLabeledEditTextPassword.EditText.InputType)

    WifiDialog.GetButton(DialogResponse.POSITIVE).Enabled = False
    Wait For (sf) Dialog_Result(res As Int)
   
    'force the keyboard to hide
    FloatLabeledEditTextSSID.EditText.Enabled = False
    FloatLabeledEditTextPassword.EditText.Enabled = False
   
    If res = DialogResponse.POSITIVE Then
        ToastMessageShow($"${FloatLabeledEditTextSSID.Text} ${FloatLabeledEditTextPassword.Text} "$, True)
    End If
End Sub

Sub CheckAllFieldsValid
    Dim valid As Boolean = False
    If FloatLabeledEditTextSSID.Text.Length > 0 Then
        If FloatLabeledEditTextPassword.Text.Length > 0 Then
            valid = True
            WifiDialog.GetButton(DialogResponse.POSITIVE).Enabled = valid
        End If
    End If
End Sub

Sub FloatLabeledEditTextSSID_TextChanged (Old As String, New As String)
    CheckAllFieldsValid
End Sub

Sub FloatLabeledEditTextPassword_TextChanged (Old As String, New As String)
    CheckAllFieldsValid
End Sub

And it still gives the same error in CheckAllFields valid.

JMB
 
Upvote 0

JMB

Active Member
Licensed User
Longtime User
Ah, just saw Erel's reply. I shall have a look at that...

JMB
 
Upvote 0

JMB

Active Member
Licensed User
Longtime User
OK, still getting the same problem even with using XUI Views.

This is what I did.
  • Added the XUI Views library to the libraries.

  • Created a layout in the designer, adding two B4XFloatTextField views, called fieldSSID, and fieldPassword.

  • Generated the members from the Designer.

  • Saved the layout as "CustomDialog"

  • Checked that the declarations appeared correctly in the B4A code.

  • Added the following code in Globals:
B4X:
Private thewifidialog As B4XDialog
  • Added the following code in Activity_Create:
B4X:
    thewifidialog.Initialize(Activity)
    thewifidialog.Title = "Wifi Credentials"

  • then changed the sub that calls the dialog to this:
B4X:
Sub btnGetWifiData_Click
 
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 300dip, 250dip)
    p.LoadLayout("CustomDialog")

    thewifidialog.PutAtTop = True 'put the dialog at the top of the screen
    Wait For (thewifidialog.ShowCustom(p, "OK", "", "CANCEL")) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        log(fieldSSID.Text & " " & fieldPassword.Text)
    End If
End Sub

And now I get
B4X:
Error occurred on line: 247 (B4XFloatTextField)
java.lang.RuntimeException: Class instance was not initialized (b4xfloattextfield)

What is odd is that line 247 is nowhere near the sub definition listed above...

What is even more weird is that the B4XFloatTextField views on the CustomDialog does not show the text that I have set them to in the Designer - they show "First Name" and "Last Name" which suggests something is getting corrupted somewhere, because I set the text to "SSID" and "Password"...

Confused.

JMB
 
Last edited:
Upvote 0

JMB

Active Member
Licensed User
Longtime User
ok... I think there's some corruption going on somewhere, because I created an entirely new layout with an entirely new name and it appears to work...
 
Upvote 0

JMB

Active Member
Licensed User
Longtime User
This is solved - or rather, sorted.

I think somehow the program is getting confused between other layouts. Not quite sure why, but when I started with a totally new layout name, it all seemed to work ok.

Are there any rules regarding layout names which might prevent this problem?

JMB
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…