Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

android - StringIndexOutOfBoundsException caused by substring

I received this crash report in my Google Play Console which I myself never experience.

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2505)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2577)
  at android.app.ActivityThread.access$1000 (ActivityThread.java:164)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1462)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:160)
  at android.app.ActivityThread.main (ActivityThread.java:5541)
  at java.lang.reflect.Method.invoke (Method.java)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:964)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:759)
Caused by: java.lang.StringIndexOutOfBoundsException: 
  at java.lang.String.startEndAndLength (String.java:504)
  at java.lang.String.substring (String.java:1333)
  at .Word.onCreate (Word.java)
  at android.app.Activity.performCreate (Activity.java:6093)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1106)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2458)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2577)
  at android.app.ActivityThread.access$1000 (ActivityThread.java:164)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1462)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:160)
  at android.app.ActivityThread.main (ActivityThread.java:5541)
  at java.lang.reflect.Method.invoke (Method.java)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:964)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:759)

This crash report doesn't mention which line of my Word.java class is causing the problem. In my class, I use the following substring method.

definition = defn.substring(0, defn.indexOf("@"));
sentence = rawMeaning.substring(rawMeaning.lastIndexOf('@') + 1);

Examples of my defn string are test1@test2, test1@. Every of my defn string string contains @. Some string contains character after @, some do not contains any character after @. My rawMeaning string share a similar format with my defn string.

It is interesting that I received another crash report which give me similar error but it mentioned the error is caused my in app billing, StringIndexOutOfBoundsException caused by in app billing queryInventoryAsync

How to solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Are you absolutely 100% sure that all of your strings contain the "@" character? As you have not shown this to be the case, that still remains the most likely cause. In fact, with the code you have shown, the only possible explanation is that your code is processing a string without this character.

I suggest releasing a new version of your app that will provide diagnostic information in the event of this error. Try this:

Change

definition = defn.substring(0, defn.indexOf("@"));

To

 try {
    definition = defn.substring(0, defn.indexOf("@"));
 } catch (Exception ex) {
    String message = "Error processing string: (" + defn + ")";
    throw new RuntimeException(message, ex);
 } 

Then the next time the app crashes, the crash log will contain a stack trace with a message telling you what string caused the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...