由子字符串引起的 StringIndexOutOfBoundsException [英] StringIndexOutOfBoundsException caused by substring

查看:37
本文介绍了由子字符串引起的 StringIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Google Play 管理中心收到了这份崩溃报告,而我自己从未体验过.

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)

这个崩溃报告没有提到我的 Word.java 类的哪一行导致了问题.在我的课程中,我使用以下 substring 方法.

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);

我的 defn 字符串示例是 test1@test2test1@.我的每个 defn 字符串都包含 @.有些字符串在@ 之后包含字符,有些字符串在@ 之后不包含任何字符.我的 rawMeaning 字符串与我的 defn 字符串共享类似的格式.

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.

有趣的是,我收到了另一个崩溃报告,它给了我类似的错误,但它提到错误是由我的应用内计费引起的,应用内计费 queryInventoryAsync 引起的 StringIndexOutOfBoundsException

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

如何解决这个问题?

推荐答案

您是否绝对 100% 确定所有字符串都包含@"字符?由于您没有证明是这种情况,这仍然是最可能的原因.事实上,对于您显示的代码,唯一可能的解释是您的代码正在处理一个没有这个字符的字符串.

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:

改变

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

 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.

这篇关于由子字符串引起的 StringIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆