使用Linkify的Android EditText崩溃

时间:2015-12-09 18:01:00

标签: android android-edittext linkify

我有一个EditText,其中包含需要为网址,电话和电子邮件进行链接的文本。我有以下xml:

   <EditText
        android:id="@+id/notes"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/page.notes.placeholder"
        android:inputType="textMultiLine"
        android:lineSpacingExtra="@dimen/margin_small"
        android:minHeight="@dimen/table_row_height"
        android:paddingLeft="@dimen/margin_normal"
        android:paddingRight="@dimen/margin_normal"
        android:layout_marginTop="@dimen/margin_xsmall"
        android:layout_marginBottom="@dimen/margin_xsmall"
        android:singleLine="false"
        android:autoLink="web|email|phone"
        android:linksClickable="true"
        android:textSize="@dimen/medium_textsize"
        />

Java中的设置是:

  EditText notes = (EditText)findViewById(R.id.notes);
  txtNotes = txtNotes.replace("\n", "<br/>");
  notes.setText(Html.fromHtml(txtNotes));
  notes.setMovementMethod(LinkMovementMethod.getInstance());
  notes.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (MotionEvent.ACTION_UP == event.getAction()) {
                if (!notesInFocus){
                    notesFocus();
                    return false;
                }
            }
            return false;
        }
    });

旧款手机(例如三星Galaxy S3mini)似乎出现问题,当点击EditText并进入编辑模式时应用程序崩溃。它不一致,但通常会长时间点击文本而崩溃。以下是堆栈跟踪:

  

12-09 14:03:50.808 3265-3265 / com.example.android E / AndroidRuntime:   致命异议:主要                                                                        java.lang.IndexOutOfBoundsException:charAt:-1&lt; 0                                                                            在   android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:112)                                                                            在android.text.Selection.setSelection(Selection.java:81)                                                                            在android.text.Selection.setSelection(Selection.java:115)                                                                            在   android.widget.Editor $ InsertionHandleView.updateSelection(Editor.java:3805)                                                                            在   android.widget.Editor $ HandleView.positionAtCursorOffset(Editor.java:3523)                                                                            在android.widget.Editor $ HandleView.updatePosition(Editor.java:3550)                                                                            在android.widget.Editor $ PositionListener.onPreDraw(Editor.java:2357)                                                                            在   android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:707)                                                                            在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1944)                                                                            在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1113)                                                                            在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4481)                                                                            在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725)                                                                            在android.view.Choreographer.doCallbacks(Choreographer.java:555)                                                                            在android.view.Choreographer.doFrame(Choreographer.java:525)                                                                            在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711)                                                                            在android.os.Handler.handleCallback(Handler.java:615)                                                                            在android.os.Handler.dispatchMessage(Handler.java:92)                                                                            在android.os.Looper.loop(Looper.java:137)                                                                            在android.app.ActivityThread.main(ActivityThread.java:4867)                                                                            at java.lang.reflect.Method.invokeNative(Native Method)                                                                            在java.lang.reflect.Method.invoke(Method.java:511)                                                                            在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1007)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)                                                                            at dalvik.system.NativeStart.main(Native Method)12-09 14:03:50.878   二〇一九年至2045年/? E / android.os.Debug:!@ Rollstate&gt; dumpstate -k -t -z -d -o   / data / log / dumpstate_app_error 12-09 14:03:50.888 2019-2074 /?   I / InputReader:触摸事件的动作是0x1(deviceType = 0)[pCnt = 1,s =]

我还注意到,如果删除Linkify,我无法复制崩溃。我想这是一个设备问题,但我收到了很多用户的Crash报告。有什么想法吗?

编辑 我想从崩溃报告中澄清一下:这个问题是三星设备独有的(使用android 4.1.2,4.2.2,4.3.5.1.1)。

0 个答案:

没有答案