字符串上的ClickableSpan不是文本

时间:2017-10-14 16:35:09

标签: java android string indexoutofboundsexception clickablespan

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

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

结果是SpecialTextView显示用户点击的任何单词。到目前为止一切都很好,但我允许用户在SpecialTextView中长按一个单词然后将其删除。我从SpecialTextView中获取一个String(称为wordtobedeleted),然后是一个String(称为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”,但我得到了崩溃。似乎我无法从包含字符串的textview获取子字符串,如果我正在读这个,就像我注释掉代码时导致崩溃的部分是我从SpecialTextView中获取String字样的地方。

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的方法,所以我很喜欢它,但是直到现在它一直很好!

1 个答案:

答案 0 :(得分:0)

  

你的问题是

     

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

  • 检查代码中的wordtobedeleted = SpecialTextView.getText().toString().substring(startSelection, endSelection);

startSelectionendSelection可能等于-1。

  • 检查代码中的String specpossibleWord = specialtappedtext.substring(specialstart, specialend);

specialstartspecialend可能等于-1。