Android Question Empty String returned from Edittext [SOLVED]

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

I have a Sub that "in Part" back spaces text in an Edittext. In some circumstances an empty string is returned from the Edittext when it still contains text.
The BS only applies to numeric values although the text can contain non-numeric Chrs. When there is only one chr or only one chr before a non-numeric chr an empty string is returned. [Crash]

I have attached all the relevent code including the Logs, any help appreciated.

Regards Roger

B4X:
Sub BtnDEL
Log("Idisplay.Text = " & Idisplay.Text)
Log("Idisplay.Text.Length = " & Idisplay.Text.Length)
                Idisplay.Text = Idisplay.Text.SubString2(0,Idisplay.Text.Length-1)  'Line 695
Log("NewIdisplay.Text = " & Idisplay.Text)
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Idisplay.SingleLine = False
    Idisplay.Wrap = False
End Sub

B4X:
Sub IDisplay_TextChanged (Old As String, New As String)
  Idisplay.SelectionStart = Idisplay.text.Length
End Sub

B4X:
** Activity (main) Create, isFirst = true **
No wakelock.
** Activity (main) Resume **
** Activity (main) Resume **
Idisplay.Text = 123+4567
Idisplay.Text.Length = 8
NewIdisplay.Text = 123+456
Idisplay.Text = 123+456
Idisplay.Text.Length = 7
NewIdisplay.Text = 123+45
Idisplay.Text = 123+45
Idisplay.Text.Length = 6
NewIdisplay.Text = 123+4
Idisplay.Text =
Idisplay.Text.Length = 0
Error occurred on line: 695 (Main)
java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=-1
    at java.lang.String.startEndAndLength(String.java:588)
    at java.lang.String.substring(String.java:1475)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
    at android.view.View.performClick(View.java:4623)
    at android.view.View$PerformClick.run(View.java:19230)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5335)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Your code will prevent the crash but an empty string is still returned from the Edittext. As shown in the Log file below, IDisplay.text = "", when it should be "123+" following a number of BtnDEL operations.
I still can't figure why.

Regards Roger



B4X:
LogCat connected to: B4A-Bridge: samsung SM-N9005
--------- beginning of /dev/log/main
Connected to B4A-Bridge (Wifi)
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
No wakelock.
** Activity (main) Resume **
Idisplay.Text = 123+4567
Idisplay.Text.Length = 8
NewIdisplay.Text = 123+456
Idisplay.Text = 123+456
Idisplay.Text.Length = 7
NewIdisplay.Text = 123+45
Idisplay.Text = 123+45
Idisplay.Text.Length = 6
NewIdisplay.Text = 123+4
Idisplay.Text =
Idisplay.Text.Length = 0
NewIdisplay.Text =
** Activity (main) Pause, UserClosed = false **
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi mc 73 and Klaus,

Sorry guys BtnDEL Sub is one that I created to simplify things [So I thought]. I should have called it BtnDEL_click.
The real Sub is below.

Regards Roger

B4X:
Sub Back_Click        ' delete the last character in input mode.
    If Settings(0,2) = 0 Then Vibrate.Vibrate (50)         ' Vibrate phone for 50 ms
    If Settings(1,2) = 1 Then
        MP.Stop
        MP.Load(File.DirAssets,"Windows XP start.wav")
        MP.Play
    End If
    If Record_flag = 1 Then   
        If shift_flag = 1 Then
            Step_count = Step_count + 1
            lblShift.Text = ""            ' Resets Shift mode to standard mode
            shift_flag = 0
            'Idisplay.Text = "n!"
           
            If Idisplay.Text = "Recording" Then Idisplay.Text = ""
            If IDispTXT_flag = 1 Then Idisplay.Text = IDispTXT
            IDispTXT_flag = 0
            Idisplay.Text = Idisplay.Text&"n!"
           
            Recording(FNum,Step_count,0) = "SUB"
            Recording(FNum,Step_count,1) = "Factorial_click" 
            Return
        End If
    End If
    If lblShift.Text = "Shift" And Record_flag = 0 Then
        Factorial_click
    Else   
        If input_flag = 1 Then
            If Buffer = "" Then Return
            If Buffer.CharAt(Buffer.Length-1) = "E" Then
               eng_flag = 0
               Eng_input_flag(1) = 0
            End If
            Buffer = Buffer.SubString2(0 ,Buffer.Length-1 )
            Rdisplay.Text = Buffer
            If Buffer = "" Then
                IntDis = 0
            Else
                   IntDis = Buffer       
             End If
            If Buffer = "" Then
                input_mode_handler(0) 
                Disp = 0
                send_to_display(Disp)
                IntDis = Disp
            End If
            If Round_Flag = 1 Then   'If rounding function is active
                IntDis = Abs(IntDis)   
                IntDis = Min(IntDis, MaxRounding)
                If Truncate_Flag = 1 Then
                    Idisplay.Text = "Truncate to " & IntDis & " decimal places"
                Else
                    Idisplay.Text = "Round to " & IntDis & " decimal places"
                End If
            End If
            If Record_flag = 1 Then
                Recording(FNum,Step_count,1) = Buffer 'Added as part of V4 mods
           
Log("Idisplay.Text = " & Idisplay.Text)
Log("Idisplay.Text.Length = " & Idisplay.Text.Length)
                'Idisplay.Text = Idisplay.Text.SubString2(0,Idisplay.Text.Length-1)
                Idisplay.Text = Idisplay.Text.SubString2(0,Max(0, Idisplay.Text.Length-1))
Log("NewIdisplay.Text = " & Idisplay.Text)

            End If
        End If
    End If
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Questions: Once you have for e.g. 123+4 and you hit "Del", are you setting somewhere the shift_flag to 1? And, if so, where is the lDispTXT taking value from?
Anyway, it's not easy, at least for me to guess, by seeing this partial code, unfortunately. Another guess could be that you're altering the lDisplay somewhere else before getting to the btnDel. A text_changed event fror e.g.
As Erel and Klaus suggested earlier, I guess that a sample project could help.
 
Upvote 0
Top