Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串而非文本上的可单击范围_Java_Android_String_Indexoutofboundsexception_Clickablespan - Fatal编程技术网

Java 字符串而非文本上的可单击范围

Java 字符串而非文本上的可单击范围,java,android,string,indexoutofboundsexception,clickablespan,Java,Android,String,Indexoutofboundsexception,Clickablespan,好的,我有一个ClickableSpan(在一个名为SpecialTextView的文本视图上),它工作得很好,它包含一个文本字符串(名为specialtabpedtext)。当另一个不同文本视图(称为BottomTextView)上的另一个ClickableSpan被长单击时,它会将单击的单词添加到SpecialTextView,如下所示- specialtappedtext = BottomTextView.getText().toString().substring(startSelect

好的,我有一个ClickableSpan(在一个名为SpecialTextView的文本视图上),它工作得很好,它包含一个文本字符串(名为specialtabpedtext)。当另一个不同文本视图(称为BottomTextView)上的另一个ClickableSpan被长单击时,它会将单击的单词添加到SpecialTextView,如下所示-

specialtappedtext = BottomTextView.getText().toString().substring(startSelection, endSelection);
SpecialTextView.append(" " + specialtappedtext);
wordtobedeleted = SpecialTextView.getText().toString().substring(startSelection, endSelection);
resultspecialtext = fullspecialtext.replace(wordtobedeleted, " ");
SpecialTextView.setText(" " + resultspecialtext);
结果是SpecialTextView显示用户点击的任何单词。到目前为止一切正常,但我允许用户长按SpecialTextView中的一个单词,然后将其删除。我从用户在SpecialTextView中点击的内容中得到一个字符串(称为wordtobedeleted),然后是一个字符串(称为fullspecialtext),它是SpecialTextView中显示的全文。然后,我替换出现在fullspecialtext中的任何wordtobedeleted实例,并在SpecialText视图中显示结果,如下所示-

specialtappedtext = BottomTextView.getText().toString().substring(startSelection, endSelection);
SpecialTextView.append(" " + specialtappedtext);
wordtobedeleted = SpecialTextView.getText().toString().substring(startSelection, endSelection);
resultspecialtext = fullspecialtext.replace(wordtobedeleted, " ");
SpecialTextView.setText(" " + resultspecialtext);
因此,如果用户点击BottomTextView中显示的单词“Bandit在客厅”,则SpecialTextView将按点击的顺序显示它们。在SpecialTextView中按下任何一个单词都会将其删除,因此如果我点击“是”、“这个”和“生活”这些单词,SpecialTextView将显示“房间里的强盗”,但相反,我会崩溃。似乎我无法从包含字符串的textview中获取子字符串,如果我正确阅读此内容,就像在注释代码时一样,则导致崩溃的部分是我从SpecialTextView中获取要删除的字符串

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我真的不太了解,所以我有点喜欢它,但到现在为止做得很好

你的问题是

java.lang.StringIndexOutOfBoundsException:length=30;索引=-1

  • 选中
    wordtobedeleted=SpecialTextView.getText().toString().substring(开始选择,结束选择)在您的代码中
开始选择
结束选择
可能等于-1

  • 选中
    String specposibleword=specialtappedtext.substring(specialstart,specialLend)在您的代码中

specialstart
speciallend
可能等于-1。

实际上,根据您的代码,您将得到“Bandit[space][space][space][space in the room”三个空格,因为您正在调用
replace
;你确定你没有用额外的空间对索引进行错误计算吗?谢谢你的回答,你对空格的看法是正确的,但我还没有弄清楚代码的这一部分实际上发生在什么地方-但是如果“”不起作用,我可以用空格。代码的工作方式是,我从不精确指定索引,理论上,代码根据单词选择的开始和结束位置计算索引。但是我认为索引是问题所在。谢谢你的回答,我不确定当代码应该根据文本选择的开始和结束位置为我计算时,索引怎么能=-1。我想知道我是否可以使用startSelection+1和endSelection+1等来获取我想要的内容。您可以在代码中添加日志。显示
子字符串
索引。感谢您的建议,这一点我目前还不太清楚,因此我将研究如何获取子字符串索引的日志。我只是不明白为什么它不起作用,当我复制并粘贴了我的第一个完美的作品。祝你好运。