Android Question error in a sub

lolo32

Member
Hello, i have a question, when a sub run (edittext textchanged) in a class i have an error and if i add an empty line (line feed) at the top of sub there is no more error?
if I modify the code of this sub or another above, I have an error again and I have to remove the second empty line at the beginning of the sub and there are no more errors ???

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 3123 (element)
java.lang.ClassCastException: anywheresoftware.b4a.objects.ListViewWrapper$SimpleListView cannot be cast to android.widget.TextView
    at b4a.example.element._exp_to_focuschanged(element.java:5570)
    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:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

B4X:
Sub exp_to_FocusChanged (HasFocus As Boolean)
    
    If (Sender=Null) Or (GetType(Sender)<>"android.widget.EditText") Then Return
    
    Dim tx As View=Sender
    
    If (tx Is EditText) And (tx.Tag="exp_to") Then
        If HasFocus Then
            foc=2
            
            Dim pan As Panel=tx.Parent
            
            For Each rx As Label In pan
                If rx.Tag="lbl_plus2" Then
                    rx.TextColor=Colors.Red
                Else
                    If rx.Tag="lbl_plus1" Or rx.Tag="lbl_plus3" Or rx.Tag="lbl_plus4" Or rx.Tag="lbl_plus5" Then  '<---------line 3123
                        rx.TextColor=Colors.Black
                    End If
                End If
            Next
        End If

    End If
    
End Sub

there is a listview on the panel but normally it should not "go up" because its tag is not the correct one.

sometimes the sender is a timer.
I specify that there are 2 timers in this class and that one of them is at 0.1s
apart from this "problem", my class is functioning very well.
I modify it to add functions ...

Thank you and good day,
laurent.

ps:excuse my english, thank you google translate ;)
 

lolo32

Member
I made a mistake, it's not "edittext textchanged" but "FocusChanged".
but I have the same error for several other sub in "text" and "focus"?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Never use ListView. Always use xCLV.
2. This code looks strange and not needed:
B4X:
If (Sender=Null) Or (GetType(Sender)<>"android.widget.EditText") Then Return
    
    Dim tx As View=Sender
    
    If (tx Is EditText) And (tx.Tag="exp_to") Then
Why would Sender be Null or something else than an EditText?

Post the relevant code and the error line.
 
Upvote 0

lolo32

Member
Hello,
I have yet another problem in my class.
I know there is a lot of code which "normally" is useless but if I don't put it I have lots of errors.

I removed everything that is useless and here is the kind of error I have:

the sub:

B4X:
Sub chk_en_CheckedChange(Checked As Boolean)

    valid_enable=Checked
    
    Dim c_en As CheckBox=Sender
    Dim p_en As Panel=c_en.Parent
    
    For Each t_en As View In p_en
        If (t_en.Tag="exp_enable") Then     <---line 980
            t_en.Enabled=Checked
        End If
    Next

End Sub

the error:

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 980 (element)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
    at b4a.example.element._chk_en_checkedchange(element.java:1190)
    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.objects.CompoundButtonWrapper$1.onCheckedChanged(CompoundButtonWrapper.java:44)
    at android.widget.CompoundButton.setChecked(CompoundButton.java:218)
    at android.widget.CompoundButton.toggle(CompoundButton.java:137)
    at android.widget.CompoundButton.performClick(CompoundButton.java:142)
    at android.view.View.performClickInternal(View.java:7425)
    at android.view.View.access$3600(View.java:810)
    at android.view.View$PerformClick.run(View.java:28305)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I do not understand anything anymore ?
 
Upvote 0

lolo32

Member
I have another error.
in a "for each v as view in p" loop (p is panel)
can "v" be null?

B4X:
Sub see_of_ValueChanged (Value As Int, UserChanged As Boolean)
  
    taille_text_off=Value
  
    Dim c_tx As SeekBar=Sender
    Dim p_tx As Panel=c_tx.Parent
  
    For Each t_tx As View In p_tx
        If (t_tx.Tag="txt_of") Then            '<---line 2346'
            Dim ty As EditText=t_tx
            ty.TextSize=Value
        End If
      
        If (t_tx.Tag="lbl_taille_of") Then
            Dim tz As Label=t_tx
            tz.Text=Value
        End If
      
    Next
  
End Sub

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 2346 (element)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
    at b4a.example.element._see_of_valuechanged(element.java:7127)
    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:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

if I add an empty line just below "sub ..." there is no more error ????

I do not understand anything anymore ...
 
Upvote 0

lolo32

Member
Hello,
I have a timer in this class with a tick at 0.1s but it is not he who calls this sub.
it is the the seekbar which calls this sub.

I create in the code a panel which contains this seekbar (and other) and other view (edittext, checkbox, ...)
a checkbox allows to "enable" all the other views on the panel.
I do not update the value of seekbar in the creation of the panel.
when i click on the checkbox which enable all the other views i have this sub which is called and the error with.

if I add an empty line at the beginning of sub, there is no more error but there is an error on the other sub for the other views (edittext, checkbox, ...)

B4X:
Sub see_of_ValueChanged (Value As Int, UserChanged As Boolean)
                            <---empty line
                            <---empty line
    taille_text_off=Value
    
    Dim c_tx As SeekBar=Sender
    Dim p_tx As Panel=c_tx.Parent
    
    For Each t_tx As View In p_tx
        If (t_tx.Tag="txt_of") Then
            Dim ty As EditText=t_tx
            ty.TextSize=Value
        End If
        
        If (t_tx.Tag="lbl_taille_of") Then
            Dim tz As Label=t_tx
            tz.Text=Value
        End If
        
    Next
    
End Sub




here is the sub which enables all the views of the panel:

B4X:
Sub chk_tx_CheckedChange(Checked As Boolean)
    
    valid_text=Checked
    
    Dim c_tx As CheckBox=Sender
    Dim p_tx As Panel=c_tx.Parent
    
    For Each t_tx As View In p_tx
        If (t_tx.Tag="valid_var") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx.Tag="txt_on") Or (t_tx.Tag="txt_of") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx.Tag="see_on") Or (t_tx.Tag="see_of") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx.Tag="bt_tx_on") Or (t_tx.Tag="bt_tx_of") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx.Tag="lbl_taille_on") Or (t_tx.Tag="lbl_taille_of") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx.Tag="ls_var_txt") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx.Tag="bt_tx_var") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx Is SeekBar) And (t_tx.Tag="see_var") Then
            Dim te As SeekBar=t_tx
            te.Enabled=Checked
        End If
        
        If (t_tx.Tag="lbl_taille_var") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx.Tag="valid_ent") Then
            t_tx.Enabled=Checked And valid_valeur
        End If
        
        If (t_tx.Tag="txt_maxi") Or (t_tx.Tag="txt_mini") Then
            t_tx.Enabled=Checked And valid_entree
        End If
        
        If (t_tx.Tag="txt_pre") Or (t_tx.Tag="txt_suf") Or (t_tx.Tag="txt_vir") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx.Tag="lbl_maxi") Or (t_tx.Tag="lbl_mini") Then
            t_tx.Enabled=Checked And valid_entree
        End If
        
        If (t_tx.Tag="lbl_pre") Or (t_tx.Tag="lbl_suf") Or (t_tx.Tag="lbl_vir") Then
            t_tx.Enabled=Checked
        End If
        
        If (t_tx.Tag="txt_exp_var") Then
            t_tx.Enabled=Checked
        End If
        
    Next
    
End Sub
 
Upvote 0

lolo32

Member
I modified all the "if ... then" and there are fewer errors but there are still some.

B4X:
Sub chk_ent_CheckedChange(Checked As Boolean)
  
    valid_entree=Checked
  
    Dim c_tx As CheckBox=Sender
    Dim p_tx As Panel=c_tx.Parent
  
    For Each t_tx As View In p_tx
      
        If "txt_maxi"=t_tx.Tag Or "txt_mini"=t_tx.Tag Or "lbl_maxi"=t_tx.Tag Or "lbl_mini"=t_tx.Tag Then
            Dim th As EditText=t_tx            'line 2449
            th.Visible=valid_entree
            th.Enabled=Checked And valid_entree
        End If
      
    Next
  
End Sub

B4X:
Error occurred on line: 2449 (element)
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
    at b4a.example.element._chk_ent_checkedchange(element.java:1214)
    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.objects.CompoundButtonWrapper$1.onCheckedChanged(CompoundButtonWrapper.java:44)
    at android.widget.CompoundButton.setChecked(CompoundButton.java:171)
    at android.widget.CompoundButton.toggle(CompoundButton.java:127)
    at android.widget.CompoundButton.performClick(CompoundButton.java:132)
    at android.view.View.performClickInternal(View.java:6582)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25897)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6692)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Where is the problem?
 
Last edited:
Upvote 0

lolo32

Member
I think I understood, I do a dim with an edittext while it is either an edittext or a label, the error is corrected.

but here is another error, the test is only on labels
I cast a listview when there is none on the panel
for info, "exp_le" is an edittext.

B4X:
Sub exp_le_FocusChanged (HasFocus As Boolean)
    
    Dim tx As EditText=Sender
    
    If "exp_le"=tx.Tag Then
        If HasFocus Then
            foc=1
            
            Dim pan As Panel=tx.Parent
            
            For Each rx As Label In pan
                If "lbl_plus1"=rx.Tag Then
                    rx.TextColor=Colors.Red
                Else
                    If "lbl_plus2"=rx.Tag Or "lbl_plus3"=rx.Tag Or "lbl_plus4"=rx.Tag Or "lbl_plus5"=rx.Tag Then      '<---line 2916'
                        rx.TextColor=Colors.Black
                    End If
                End If
            Next
        End If

    End If
    
End Sub

B4X:
Error occurred on line: 2916 (element)
java.lang.ClassCastException: anywheresoftware.b4a.objects.ListViewWrapper$SimpleListView cannot be cast to android.widget.TextView
    at b4a.example.element._exp_le_focuschanged(element.java:5189)
    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:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 
Upvote 0
Top