-1

好的,我有一个运行良好的 ClickableSpan(在名为 SpecialTextView 的 TextView 上),它包含一个文本字符串(称为 specialtappedtext)。当另一个TextView(称为BottomTextView)上的另一个ClickableSpan被长按时,它会将单击的单词添加到SpecialTextView,如下所示 -

specialtappedtext = BottomTextView.getText().toString().substring(startSelection, endSelection);
SpecialTextView.append(" " + specialtappedtext);

结果是 SpecialTextView 显示用户点击的任何单词。到目前为止一切都很好,但是我允许用户长按 SpecialTextView 中的一个单词,然后将其删除。我从用户在 SpecialTextView 中点击的内容中得到一个字符串(称为 wordtobedeleted),然后是一个字符串(称为 fullspecialtext),它是出现在 SpecialTextView 中的整行文本。然后我替换出现在 fullspecialtext 中的任何 wordtobedeleted 实例,并将结果显示在 SpecialTextView 中,如下所示 -

wordtobedeleted = SpecialTextView.getText().toString().substring(startSelection, endSelection);
resultspecialtext = fullspecialtext.replace(wordtobedeleted, " ");
SpecialTextView.setText(" " + resultspecialtext);

因此,如果用户点击 BottomTextView 中显示的“Bandit is in the living room”字样,则 SpecialTextView 将按照点击的顺序显示它们。在 SpecialTextView 中按下这些词中的任何一个都应该删除它们,所以如果我点击“is”、“the”和“living”这些词,SpecialTextView 将显示“Bandit in room”,但我会崩溃。似乎我无法从包含字符串的文本视图中获取子字符串,如果我正在阅读此内容,因为当我注释掉代码时,导致崩溃的部分是我从 SpecialTextView 获取字符串 wordtobedeleted 的地方。

ClickableSpan,对一些冗长的名字感到抱歉,它可以帮助我一目了然地跟踪是什么,因为我正在处理另一个具有几乎相同名称的 ClickableSpan 等 -

 SpecialTextView.setMovementMethod(LinkMovementMethod.getInstance());
 SpecialTextView.setText(specialtappedtext, TextView.BufferType.SPANNABLE);
 Spannable specialspans = (Spannable) SpecialTextView.getText();
 BreakIterator specialiterator = BreakIterator.getWordInstance(Locale.UK);
 specialiterator.setText(specialtappedtext);
 int specialstart = specialiterator.first();
 for (int specialend = specialiterator.next(); specialend != BreakIterator.DONE; specialstart = specialend, specialend = specialiterator
            .next()) {
     String specpossibleWord = specialtappedtext.substring(specialstart, specialend);
     if (Character.isLetterOrDigit(specpossibleWord.charAt(0))) {
            ClickableSpan specialclickSpan = getSpecialClickableSpan(specpossibleWord);
         specialspans.setSpan(specialclickSpan, specialstart, specialend,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     }
 }

不管它是否有帮助,但这是错误日志的内容-

 Process: com.example.warrenanna.game003, PID: 18321
                                                                            java.lang.StringIndexOutOfBoundsException: length=30; index=-1
                                                                                at java.lang.String.substring(String.java:1926)
                                                                                at com.example.warrenanna.game003.MainActivity$2.onLongClick(MainActivity.java:133)
                                                                                at android.view.View.performLongClickInternal(View.java:5762)
                                                                                at android.view.View.performLongClick(View.java:5720)
                                                                                at android.widget.TextView.performLongClick(TextView.java:9427)
                                                                                at android.view.View.performLongClick(View.java:5738)
                                                                                at android.view.View$CheckForLongPress.run(View.java:22450)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:241)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6281)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我对编程方面的任何先进技术都是新手,而且我真的不知道如何使用 ClickableSpan,所以我有点想知道它,但直到现在都做得很好!

4

1 回答 1

0

你的问题是

java.lang.StringIndexOutOfBoundsException: length=30; index=-1

  • wordtobedeleted = SpecialTextView.getText().toString().substring(startSelection, endSelection);入您的代码。

startSelection并且endSelection可能等于 -1 。

  • String specpossibleWord = specialtappedtext.substring(specialstart, specialend);入您的代码。

specialstart并且specialend可能等于-1。

于 2017-10-14T16:51:23.867 回答