Android Question Date input mask error

Makumbi

Well-Known Member
Licensed User
Please help below are codes for the in put mask
B4X:
Sub MaskedEditText2_FocusChanged(HasFocus As Boolean)
    If HasFocus Then
        'Log("Focus gain: ET1")
        MaskedEditText2.SelectionStart = 11
    Else
        Log("Focus loss: ET1")
        'Is the date valid?
        DateTime.DateFormat = "dd/MM/yyyy"
        Try
            DateTime.DateParse(MaskedEditText2.Text.SubString2(11, 21))
        Catch
            MaskedEditText2.ShowError("Invalid date")
            Return
        End Try
        
    End If
End Sub

Sub MaskedEditText1_FocusChanged(HasFocus As Boolean)
    If HasFocus Then
        'Log("Focus gain: ET1")
        MaskedEditText1.SelectionStart = 11
    Else
        Log("Focus loss: ET1")
        'Is the date valid?
        DateTime.DateFormat = "dd/MM/yyyy"
        Try
            DateTime.DateParse(MaskedEditText2.Text.SubString2(11, 21))
        Catch
            MaskedEditText1.ShowError("Invalid date")
            Return
        End Try
        
    End If
End Sub

This is the error that is generated after putting 21/12/2018
my date format is dd/MM/yyyy
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
ExecuteHtml: SELECT DISTINCT Names,Class || Stream As CS,Amount As Balance FROM Balances
** Activity (main) Pause, UserClosed = false **
** Activity (accountstatement) Create, isFirst = true **
ExecuteHtml: SELECT DISTINCT Names,Class || Stream As CS,Amount As Balance FROM Balances
** Activity (accountstatement) Resume **
Focus loss: ET1
Error occurred on line: 203 (Accountstatement)
java.lang.StringIndexOutOfBoundsException: length=10; regionStart=11; regionLength=10
    at java.lang.String.substring(String.java:1931)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:352)
    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.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at flm.b4a.maskededittext.MaskedEditTextWrapper$2.onFocusChange(SourceFile:84)
    at android.view.View.onFocusChanged(View.java:6180)
    at android.widget.TextView.onFocusChanged(TextView.java:8400)
    at android.view.View.clearFocusInternal(View.java:6062)
    at android.view.View.unFocus(View.java:6095)
    at android.view.ViewGroup.requestChildFocus(ViewGroup.java:734)
    at android.view.View.handleFocusGainInternal(View.java:5931)
    at android.view.View.requestFocusNoSearch(View.java:9062)
    at android.view.View.requestFocus(View.java:9041)
    at android.view.View.requestFocus(View.java:9008)
    at android.view.View.requestFocus(View.java:8987)
    at android.view.View.onTouchEvent(View.java:11112)
    at android.widget.TextView.onTouchEvent(TextView.java:8459)
    at android.view.View.dispatchTouchEvent(View.java:9943)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:411)
    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810)
    at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
    at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:373)
    at android.view.View.dispatchPointerEvent(View.java:10163)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4434)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6210)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6184)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6145)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6313)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:323)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.inter
Message longer than Log limit (4000). Message was truncated.
 

Attachments

  • Screenshot_1545398729.png
    Screenshot_1545398729.png
    59.5 KB · Views: 226

roberto64

Active Member
Licensed User
Longtime User
B4X:
dim miastringa as string ="2018/02/12"
LblScadenza.Text = DateTime.Date(ConvertSQLDate(miastringa)

Sub ConvertSQLDateIta(s As String) As Long
    DateTime.DateFormat = "dd/MM/yyyy"
   Dim ticks As Long = DateTime.DateParse(s)
    DateTime.DateFormat = "yyyy-MM-dd"

     Return ticks
End Sub
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
The error:

java.lang.StringIndexOutOfBoundsException: length=10; regionStart=11; regionLength=10

is because you're trying to get a substring from position 11 in a string that is only 10 chars long. If the value in the MaskedEditText is "21/12/2018" that is 10 chars, so not sure why you're doing a SubString starting at char 11...

- Colin.
 
Upvote 0
Top