安卓。 charAt (setSelection) 中的奇怪崩溃

Posted

技术标签:

【中文标题】安卓。 charAt (setSelection) 中的奇怪崩溃【英文标题】:Android. Strange crash in charAt (setSelection) 【发布时间】:2013-12-11 21:01:45 【问题描述】:

我在应用程序中有一个非常奇怪的崩溃。 堆栈跟踪:

java.lang.IndexOutOfBoundsException: charAt: -1 < 0
at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:112)
at android.text.Selection.setSelection(Selection.java:84)
at android.text.Selection.setSelection(Selection.java:127)
at android.widget.Editor$InsertionHandleView.updateSelection(Editor.java:4971)
at android.widget.Editor$HandleView.positionAtCursorOffset(Editor.java:4647)
at android.widget.Editor$HandleView.updatePosition(Editor.java:4675)
at android.widget.Editor$PositionListener.onPreDraw(Editor.java:2714)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:711)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2097)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1179)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4859)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:664)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

我的应用程序包(类/方法)上没有任何链接的堆栈跟踪。 有没有人遇到过这个问题?谢谢。

编辑

您能看到,堆栈跟踪在我的应用程序的包/类上没有任何链接,但应用程序还是崩溃了。

UPD 我使用 Selection.setSelection。我不使用 SpannableStringBuilder。

@Override
public void afterTextChanged(Editable s) 
    if (!s.toString().isEmpty()) 
        Selection.setSelection(s, s.length());
       

【问题讨论】:

你在为 charAt(index) 传递什么? 请给我们看一些代码 IndexOutOfBoundsException 你能把 s.length 改成 s.length-1 并检查一下吗? 【参考方案1】:

这似乎是一个操作系统错误,需要某个操作系统版本(例如 4.1.2)和某个设备(例如 Galaxy S3 I9300)的组合。这不是你的问题。不幸的是,我也不知道如何解决这个问题。

我很好奇您是如何遇到这个问题的。根据调用堆栈,我的猜测是您点击文本视图并尝试从剪贴板粘贴文本。

【讨论】:

我遇到了类似的问题。帖子在这里:***.com/questions/27447117/… 我在三星设备中也面临同样的问题。有什么解决方法吗?【参考方案2】:

阅读第一行...上面写着java.lang.IndexOutOfBoundsException: charAt: -1 &lt; 0。问题是您的字符串没有您要搜索的字符。贴出代码。那我们看看有什么问题……

【讨论】:

你看,堆栈跟踪在我的应用程序的包/类上没有任何链接,但应用程序还是崩溃了。 好吧,你在哪里使用SpannableStringBuilder @Override public void afterTextChanged(Editable s) if (!s.toString().isEmpty()) Selection.setSelection(s, s.length());

以上是关于安卓。 charAt (setSelection) 中的奇怪崩溃的主要内容,如果未能解决你的问题,请参考以下文章

EditText的setSelection属性

listview.setSelection()

jQuery:jCrop 'setSelect' 根本没有设置?

android listview.setselection(position)的作用是啥

Swift - UIButton 覆盖 setSelected

UITableViewCell 选择器 setSelected:animated: 被调用多次?