Android Question xCustomListView crash

mik21

Member
I have simple activity which has xCustomListView with panel with edit view. Edit has FocusChanged proc. This proc read edit value and set label text in picture edit value is "12" and label text is "Kid 0".
Screenshot.jpg


FocusChanged:
Private Sub kidEdit_FocusChanged (HasFocus As Boolean)
    If Not (HasFocus) Then        'leave from Edit
        Dim index As Int = CLV1.GetItemFromView(Sender)
        Dim record As listBox = CLV1.GetValue(index)
        Dim krabica As String = record.kidEdit.Text
        If krabica = "" Then    'pre nezadanu krabicu vymaz label
            record.kidLabel.Text = ""
        Else
            Dim nazovLieku As String = "kid " & index
            record.kidLabel.Text = nazovLieku
            Log($"Kid edit record No. ${index} "$)
        End If
    End If
End Sub
The "Clear CLV" button clear xCustomListView. If the cursor is parking on the edit view and I click on "Clear CLV" app crash. Debbuger show me that clear clv is run before focus change proc. Crash is on focuschanged proc 2 line "Dim index As Int = CLV1.GetItemFromView(Sender)"
CLV was cleared
Error occurred on line: 84 (CLVActivity)
java.lang.RuntimeException: Object should first be initialized (B4XView).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at b4a.example3.customlistview._getitemfromview(customlistview.java:413)
at b4a.example.clvactivity._kidedit_focuschanged(clvactivity.java:504)
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:348)
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 anywheresoftware.b4a.BA$1.run(BA.java:352)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)


How to I avoid this crash ?
 

Attachments

  • Screenshot.jpg
    Screenshot.jpg
    31.8 KB · Views: 85
  • Example.zip
    12.6 KB · Views: 83

José J. Aguilar

Expert
Licensed User
You can try with

B4X:
Private Sub kidEdit_FocusChanged (HasFocus As Boolean)
    If Not (HasFocus) Then        'leave from Edit
        If CLV1.Size > 0 Then '<--------------------
            Dim index As Int = CLV1.GetItemFromView(Sender)
            Dim record As listBox = CLV1.GetValue(index)
            Dim krabica As String = record.kidEdit.Text
            If krabica = "" Then    'pre nezadanu krabicu vymaz label
                record.kidLabel.Text = ""
            Else
                Dim nazovLieku As String = "kid " & index
                record.kidLabel.Text = nazovLieku
                Log($"Kid edit record No. ${index} "$)
            End If
        End If
    End If
End Sub
 
Upvote 0

mik21

Member
Thanks for answer. Your advice is a partially solution. Problem is on the next detail:
The algorithm idea is: user can create list of records and he can repeat the process. So I design the CLV, where every panel is the "record".
If the user create list and he want to start again, the program clear CLV and create new panel. If the new panel is created, CLV1.Size return 1 and sub FocusChanged crashed.
Problem is that FocusChanged run on nonexisting record, because the last CLV was cleared, then is created the new CLV with new panel (record)

I think the problem is in inappropriate sequence of sub. Current sequence is:
1. external event to CLV - clear CLV
2. FocusChanged Check (if CLV was cleared it has no sense)
 
Upvote 0

José J. Aguilar

Expert
Licensed User
Hi:

because the last CLV was cleared, then is created the new CLV with new panel
This was not happening in your example, so my solution was working.
If I add "CreatePanel" after CLV1.Clear, then I got the error.

A possible solution is create a "flag" to know if the CLV has been cleared or not.

Try this example
 

Attachments

  • Example.zip
    12.7 KB · Views: 88
Upvote 0

mik21

Member
I am sorry, I was hurry up to create example from original application. Solution with flag will be usable. I posted this message because I wanted to avoid the flag
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
ry this example
Nice example Jose. I was wondering if you set the event name the same for both Edittext views and call it Edit, you might be able to achieve what you are trying to do with one sub. I do not know if it makes sense or not:

B4X:
Private Sub Edit_FocusChanged (HasFocus As Boolean)
    Log("Focus midEdit or kidEdit changed")
    If Not (HasFocus) Then        'leave from Edit
        If Not(Cleared) Then
            Dim index As Int = CLV1.GetItemFromView(Sender)
            Dim record As listBox = CLV1.GetValue(index)
            Dim krabica As String = record.midEdit.Text
            If krabica = "" Then    'pre nezadanu krabicu vymaz label
                record.kidLabel.Text = ""
            Else
                Dim nazovPozicie As String = "position for index " & index
                record.midLabel.Text = nazovPozicie
                Log($"Position for record No. ${index} "$)
                Log("Pozicle: " & nazovPozicie)
                
                Dim nazovLieku As String = "kid " & index
                record.kidLabel.Text = nazovLieku
                Log($"Kid edit record No. ${index} "$)
                Log("Lieku: " & nazovLieku)
            End If
        End If
    End If
    Cleared = False
End Sub
 
Upvote 0
Top