gpt4 book ai didi

java - 当试图用一个字符替换两个字符时,应用程序崩溃。它可以很好地替换一个字符

转载 作者:行者123 更新时间:2023-11-30 02:22:37 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我很难用一个字符替换两个字符。它在仅替换一个字符时有效,但在同时用一个字符替换两个字符时失败。我发现应用程序可以正确地用两个新字符替换两个字符。

能否请您帮我指出问题所在以及如何修复我的代码?

tt = new TextWatcher() {
public void afterTextChanged(Editable s){
et.setSelection(s.length()); }
public void beforeTextChanged(CharSequence s,int start,int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(tt);
String tvalue = et.getText().toString();
tvalue = tvalue.replaceAll("k", "1");;
tvalue = tvalue.replaceAll("1h", "2");
//THE first line replaces "k" with "1" WORKS FINE BUT THE second line for replacing "1h" with "2" DOES NOT. IT CRASHES. I found that it is possible to replace "1h" with "2 "(two followed by space)!
et.addTextChangedListener(tt);
}
};
et.addTextChangedListener(tt);
}
setSelection(s.length());

提前致谢。这是我在这个网站上的第一个问题。

下面是logcat

02-02 09:39:27.674      838-838/in.healthy100.gujjuwriter E/MessageQueue-JNI﹕ java.lang.IndexOutOfBoundsException: setSpan (10 ... 10) ends beyond length 9
at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
at android.text.Selection.setSelection(Selection.java:76)
at android.text.Selection.setSelection(Selection.java:87)
at android.widget.EditText.setSelection(EditText.java:94)
at in.healthy100.gujjuwriter.MyActivity$1.onTextChanged(MyActivity.java:270)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.handleTextChanged(TextView.java:7467)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9183)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:223)
at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
at android.widget.TextView.doKeyDown(TextView.java:5532)
at android.widget.TextView.onKeyDown(TextView.java:5343)
at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
at android.view.View.dispatchKeyEvent(View.java:7665)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3852)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3558)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3718)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.j
02-02 09:39:27.674 838-838/in.healthy100.gujjuwriter D/AndroidRuntime﹕ Shutting down VM

最佳答案

这是导致错误的代码:

setSelection(s.length());

您正试图使用​​原始字符串的长度将光标设置到字符串的末尾。当您运行此代码时:

tvalue = tvalue.replaceAll("k", "1");

字符串的长度没有变化,所以使用原来的长度就可以了。但是,当您运行这段代码时:

tvalue = tvalue.replaceAll("1h", "2");

字符串的长度将减少,因为您将 2 个字符替换为 1 个字符。因此,使用原始字符串的长度调用 setSelection() 将导致越界错误,因为您正试图将光标移到字符串末尾之外。

解决方案是在替换完成后使用新字符串的长度调用 setSelection():

setSelection(tvalue.length());

关于java - 当试图用一个字符替换两个字符时,应用程序崩溃。它可以很好地替换一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28271103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com