Java 如何在android中解决StringIndexOutOfBoundsException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22955414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:36:24  来源:igfitidea点击:

how to resolve StringIndexOutOfBoundsException in android

javaandroidindexoutofboundsexception

提问by Farhan Shah

This is my logcat report:

这是我的 logcat 报告:

java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=2
at java.lang.String.startEndAndLength(String.java:583)
at java.lang.String.substring(String.java:1464)
at com.buzzador.profile.getValidPhoneNumber(profile.java:1966)
at com.buzzador.profile.setDataForServer(profile.java:1717)
at com.buzzador.profile.onClick(profile.java:236)
at android.view.View.performClick(View.java:4377)
at android.view.View$PerformClick.run(View.java:18044)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

and i think the issue is in this function:

我认为问题出在这个函数中:

public String getValidPhoneNumber (String phoneNumber, String country)
{
    String csValidPhoneNumber = "";

    phoneNumber = getPhoneNumberWithoutReqularExpresions(phoneNumber);
    phoneNumber = phoneNumber.replaceFirst ("^0*", "");

    String csCountryCode = getCountryCode(country);

    String csAppendedCode = phoneNumber.substring(0, csCountryCode.length());
    if(csAppendedCode.equals(csCountryCode))
    {
        csValidPhoneNumber = "+" + phoneNumber;
        return csValidPhoneNumber;
    }

    csValidPhoneNumber = "+" + csCountryCode + phoneNumber;

    return csValidPhoneNumber;
}

采纳答案by Kevin Robatel

Are you sure that phoneNumberis not equal to ""? You have to check that phoneNumberhave more chars than the csCountryCode.length().

你确定这phoneNumber不等于""?您必须检查是否phoneNumber有比csCountryCode.length().

String csAppendedCode = phoneNumber.length() > csCountryCode.length() ? phoneNumber.substring(0, csCountryCode.length()) : "";

回答by Hamid Shatu

The last index of a Stringis string.length()-1but you are trying to access index with string.length()which doesn't exist, that's why you are getting this exception...

a 的最后一个索引String是,string.length()-1但您正在尝试访问string.length()不存在的索引,这就是您收到此异常的原因...

java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=2

Now, you should change this line...

现在,您应该更改此行...

String csAppendedCode = phoneNumber.substring(0, csCountryCode.length());

to this...

到这...

String csAppendedCode = phoneNumber.substring(0, csCountryCode.length()-1);