致命异常:java.lang.StringIndexOutOfBoundsException

时间:2018-09-14 06:30:06

标签: java android

在我的应用程序中,我发送了一些字符串,并让每个首字母大写字母大写,并且效果很好,但对于某些用户而言,此方法导致应用程序崩溃。我不知道是什么原因导致崩溃,请在下面为我提供详细信息。

错误

Thread-1891
       at java.lang.Object.wait(Object.java)
       at java.lang.Thread.parkFor$(Thread.java:1220)
       at sun.misc.Unsafe.park(Unsafe.java:299)
       at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2013)
       at java.util.concurrent.PriorityBlockingQueue.take(PriorityBlockingQueue.java:510)
       at jd.a(:com.google.android.gms.dynamite_adsdynamite@13280046@13.2.80 (040306-211705629):8)
       at jd.run(:com.google.android.gms.dynamite_adsdynamite@13280046@13.2.80 (040306-211705629):64)

发生错误的代码

   public static String capitalize(@NonNull String input) {

        String[] words = input.toLowerCase().split(" ");
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < words.length; i++) {
            String word = words[i];

            if (i > 0 && word.length() > 0) {
                builder.append(" ");
            }

            String cap = word.substring(0, 1).toUpperCase() + word.substring(1); //This is the line causing app crash

            builder.append(cap);
        }
        return builder.toString();
    }

堆栈跟踪

Fatal Exception: java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=1
       at java.lang.String.startEndAndLength(String.java:298)
       at java.lang.String.substring(String.java:1087)
       at com.cbcwebs.app.wincoin.UserFragment.capitalize(UserFragment.java:64)
       at com.cbcwebs.app.wincoin.UserFragment$1.onDataChange(UserFragment.java:89)
       at com.google.firebase.database.zzp.onDataChange(Unknown Source)
       at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
       at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
       at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:7325)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

2 个答案:

答案 0 :(得分:4)

此行导致应用程序崩溃,因为可能会出现单词长度为1的情况,而您正在访问类似word.substring(1)这样的单词的第二个字符。

这是更正的代码

if(word.length() > 1)
String cap = word.substring(0, 1).toUpperCase() + word.substring(1); 
else
String cap = word.substring(0, 1).toUpperCase();

答案 1 :(得分:-2)

  public static String capitalize(@NonNull String input) {

        String[] words = input.toLowerCase().split(" ");
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < words.length-1; i++) {
            String word = words[i];

            if (i > 0 && word.length() > 0) {
                builder.append(" ");
            }

            String cap = word.substring(0, 1).toUpperCase() + word.substring(1); //This is the line causing app crash

            builder.append(cap);
        }
        return builder.toString();
    }

将lenght-1添加到循环迭代中