EditText StringIndexOutOfBoundsException

时间:2015-12-21 20:05:22

标签: android android-edittext

我有一个带属性的EditText

  

textMultiLine

如果用户键入单词,则应用程序会因此异常而崩溃

12-21 20:40:50.285 30502-30502/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               java.lang.IndexOutOfBoundsException
                                                   at android.graphics.Paint.getTextRunAdvances(Paint.java:2175)
                                                   at android.text.TextLine.handleText(TextLine.java:988)
                                                   at android.text.TextLine.handleRun(TextLine.java:1146)
                                                   at android.text.TextLine.measureRun(TextLine.java:636)
                                                   at android.text.TextLine.measure(TextLine.java:499)
                                                   at android.text.Layout.getHorizontal(Layout.java:868)
                                                   at android.text.Layout.getHorizontal(Layout.java:845)
                                                   at android.text.Layout.getPrimaryHorizontal(Layout.java:820)
                                                   at android.text.Layout.getCursorPath(Layout.java:1343)
                                                   at android.widget.TextView.getUpdatedHighlightPath(TextView.java:5770)
                                                   at android.widget.TextView.onDraw(TextView.java:5972)
                                                   at android.view.View.draw(View.java:14853)
                                                   at android.view.View.getDisplayList(View.java:13744)
                                                   at android.view.View.getDisplayList(View.java:13786)
                                                   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3290)
                                                   at android.view.View.getDisplayList(View.java:13680)
                                                   at android.view.View.getDisplayList(View.java:13786)
                                                   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3290)
                                                   at android.view.View.getDisplayList(View.java:13680)
                                                   at android.view.View.getDisplayList(View.java:13786)
                                                   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3290)
                                                   at android.view.View.getDisplayList(View.java:13680)
                                                   at android.view.View.getDisplayList(View.java:13786)
                                                   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3290)
                                                   at android.view.View.getDisplayList(View.java:13680)
                                                   at android.view.View.getDisplayList(View.java:13786)
                                                   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3290)
                                                   at android.view.View.getDisplayList(View.java:13680)
                                                   at android.view.View.getDisplayList(View.java:13786)
                                                   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3290)
                                                   at android.view.View.getDisplayList(View.java:13680)
                                                   at android.view.View.getDisplayList(View.java:13786)
                                                   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3290)
                                                   at android.view.View.getDisplayList(View.java:13680)
                                                   at android.view.View.getDisplayList(View.java:13786)
                                                   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3290)
                                                   at android.view.View.getDisplayList(View.java:13680)
                                                   at android.view.View.getDisplayList(View.java:13786)
                                                   at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1411)
                                                   at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1359)
                                                   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2672)
                                                   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2538)
                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2154)
                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:591)
                                                   at android.view.Choreographer.doFrame(Choreographer.java:561)
                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
                                                   at android.os.Handler.handleCallback(Handler.java:730)
                                                   at android.os.Handler.dispatchMessage(Handler.java:92)
                                                   at android.os.Looper.loop(Looper.java:137)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:525)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
                                                   at dalvik.system.NativeStart.main(Native Method)

EditText的布局是

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="88dp"
        android:isScrollContainer="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:gravity="left|top"
        android:inputType="textMultiLine"
        android:textAppearance="?android:attr/textAppearanceButton"
        android:hint="@string/Text"
        android:background="@drawable/edittextstyle"
        android:padding="10dp"
        android:textColor="@color/DARK_TEXT_COLOR" >
    </EditText>

没有编辑EditText的代码。只是布局。 如果我将inputType属性更改为

  

文本

应用程序不会崩溃。 我错了什么?

1 个答案:

答案 0 :(得分:1)

从此处更改您的代码:

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="88dp"
        android:isScrollContainer="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:gravity="left|top"
        android:inputType="textMultiLine"
        android:textAppearance="?android:attr/textAppearanceButton"
        android:hint="@string/Text"
        android:background="@drawable/edittextstyle"
        android:padding="10dp"
        android:textColor="@color/DARK_TEXT_COLOR" >
    </EditText>

To This:

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="88dp"
        android:isScrollContainer="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:gravity="left|top"
        android:inputType="textMultiLine"
        android:hint="@string/Text"
        android:background="@drawable/edittextstyle"
        android:padding="10dp"
        android:textColor="@color/DARK_TEXT_COLOR" >
    </EditText>

导致问题的xml代码的这一部分:

android:textAppearance="?android:attr/textAppearanceButton"

希望它有用