我在Android Studio中使用substring时遇到StringIndexOutOfBoundsException

时间:2016-01-15 06:44:09

标签: android string android-studio

这行代码将我的三个输入用于电话号码(输入将电话号码分成区号,前三位数和后四位数)放入字符串fullPhoneNumber中。在询问之前我查看了几个答案,但我无法使它们适应我的代码。

fullPhoneNumber = areaCodeInput.getText().toString() + firstThreeDigitsInput.getText().toString() + finalFourDigitsInput.getText().toString();

接下来,此代码将完整字符串输入数据库。

 LoginEntries newPhoneNumber = new LoginEntries("", "", "", "","", fullPhoneNumber);

当我想在活动中打印电话号码时,会调用此段代码。我在这种方法中使用substring以格式(888-888-8888)显示电话号码。但总有一个错误。 dbString ='('+ holder.substring(1,3)+')'+' - '+ holder.substring(4,3)+' - '+ holder.substring(7,4);

public String phoneNumberDatabaseToString(){
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE 1";

    //cursor point to a location in your results
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    while(!c.isAfterLast()){
        if(c.getString(c.getColumnIndex(COLUMN_PHONENUMBER)) != null){
            String holder = c.getString(c.getColumnIndex(COLUMN_PHONENUMBER));
            dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 3) + '-' + holder.substring(7, 4);
            dbString += "\n";
        }
        c.moveToNext();
    }
    db.close();
    c.close();
    return dbString;
}

如果我在stackoverflow上找到子字符串,那么这是错误。

     Process: com.example.vitaliy_2.emailpassworddatabasetrial, PID: 17397
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5191)                                                                                                           at android.view.View$PerformClick.run(View.java:20916) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95)                                                                                                           at android.os.Looper.loop(Looper.java:145)                                                                                                           at android.app.ActivityThread.main(ActivityThread.java:5972                                                                                                          at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Caused by: java.lang.StringIndexOutOfBoundsException: length=12; regionStart=4; regionLength=-1
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at com.example.vitaliy_2.emailpassworddatabasetrial.EmailDBHandler.phoneNumberDatabaseToString(EmailDBHandler.java:257)
    at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.printDatabase(SignUpMainClass.java:183)
    at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.registerAccount(SignUpMainClass.java:153)
    at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
at android.view.View.performClick(View.java:5191) 
at android.view.View$PerformClick.run(View.java:20916)                                                                                                           at android.os.Handler.handleCallback(Handler.java:739)                                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5972) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.Zyg`enter code here`oteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

我改变了代码,希望它有所帮助,但无济于事。 dbString ='('+ holder.substring(1,3)+')'+' - '+ holder.substring(4,6)+' - '+ holder.substring(7,10);

 public String phoneNumberDatabaseToString(){
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE 1";

        //cursor point to a location in your results
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();
        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex(COLUMN_PHONENUMBER)) != null){
                String holder = c.getString(c.getColumnIndex(COLUMN_PHONENUMBER));
                dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 6) + '-' + holder.substring(7, 10);
                dbString += "\n";
            }
            c.moveToNext();
        }
        db.close();
        c.close();
        return dbString;
    }

这是我从这个特定代码中得到的错误。

Process: com.example.vitaliy_2.emailpassworddatabasetrial, PID: 11254
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5191) 
at android.view.View$PerformClick.run(View.java:20916) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5972) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Caused by: java.lang.StringIndexOutOfBoundsException: length=12; regionStart=4; regionLength=-1
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at com.example.vitaliy_2.emailpassworddatabasetrial.EmailDBHandler.phoneNumberDatabaseToString(EmailDBHandler.java:257)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.printDatabase(SignUpMainClass.java:183)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.registerAccount(SignUpMainClass.java:153)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
at android.view.View.performClick(View.java:5191) 
at android.view.View$PerformClick.run(View.java:20916) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5972) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

感谢您阅读

2 个答案:

答案 0 :(得分:3)

您应该仔细阅读方法说明。 String.substring (int start, int end)参数为startend,而不是startlength
所以用它作为  holder.substring(4, 3)你尝试从第4个开始获取子字符串,并以第3个字符结尾,这个子字符串的长度自然是-1

答案 1 :(得分:0)

字符串索引是0,holder.substring(1,3)应该是holder.substring(0,3)等。(第一个参数是包含的,第二个参数是0到3的独有意味着它将获得索引0 ,1和2)