Android - 当我写东西时,EditText 给出 IndexOutOfBounds 异常

标签 android android-edittext

我正在尝试做一个简单的 fragment ,其中包含一个评级字段和一个用于评论的 EditText。当我单击 EditText 字段并键入内容时出现问题。此时,应用程序崩溃。

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TableLayout
    android:id="@+id/tableLayoutReq"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <TableRow
        android:id="@+id/tableRowReq1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/text_view_acceptance"
            android:text="@string/label_acceptance"
            android:textStyle="bold"
            android:layout_gravity="center"
            android:layout_weight="0.5" />

        <RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:numStars="5"/>
    </TableRow>
</TableLayout>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/label_payment_restrictions_req"
    android:text="@string/label_comment"
    android:textStyle="bold"
    android:gravity="start"
    android:layout_marginTop="10dp" />

<EditText
    android:gravity="top"
    android:id="@+id/edit_text_comment_req"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:maxHeight="300dp"
    android:paddingStart="0dp"
    android:paddingLeft="0dp"
    android:layout_marginTop="5dp"
    android:padding="5dp"
    android:background="@drawable/evaltext_bg" />

<Button
    android:id="@+id/button_evaluate_service"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_marginTop="10dp"
    android:text="@string/button_evaluate"
    style="@style/button_eval" />

</LinearLayout>

日志错误:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException
     at android.graphics.Paint.getRunAdvance(Paint.java:2346)
     at android.text.TextLine.handleText(TextLine.java:748)
     at android.text.TextLine.handleRun(TextLine.java:898)
     at android.text.TextLine.measureRun(TextLine.java:417)
     at android.text.TextLine.measure(TextLine.java:296)
     at android.text.Layout.getHorizontal(Layout.java:929)
     at android.text.Layout.getHorizontal(Layout.java:907)
     at android.text.Layout.getPrimaryHorizontal(Layout.java:882)
     at android.text.Layout.getPrimaryHorizontal(Layout.java:872)
     at android.widget.Editor$CursorAnchorInfoNotifier.updatePosition(Editor.java:3460)
     at android.widget.Editor$PositionListener.onPreDraw(Editor.java:2506)
     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
     at android.view.Choreographer.doFrame(Choreographer.java:606)
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5417)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Java 类

public class RequestEvaluation extends Fragment {

    private RatingBar ratingBar;
    private Button button;
    private EditText commentsET;

    public RequestEvaluation() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_request_evaluation, container, false);

        commentsET = (EditText) rootView.findViewById(R.id.edit_text_comment_req);
        commentsET.setInputType(InputType.TYPE_CLASS_TEXT |
                InputType.TYPE_TEXT_FLAG_MULTI_LINE);


        //addListenerOnRatingBar(rootView);
        //addListenerOnButton(rootView);

        return rootView;
    }

    public void addListenerOnRatingBar(View rootView) {

        ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);

        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            }
        });
    }

    public void addListenerOnButton(View rootView) {

        ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);
        button = (Button) rootView.findViewById(R.id.button_evaluate_service);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity().getApplicationContext(),
                        String.valueOf(ratingBar.getRating()), Toast.LENGTH_LONG).show();
            }

        });

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

有人遇到过类似的问题吗?或者有人知道会发生什么吗?

最佳答案

请在您的编辑 TextView 中添加以下代码

android:inputType="text" 

android:inputType="textMultiLine"

关于Android - 当我写东西时,EditText 给出 IndexOutOfBounds 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35556969/

相关文章:

java - 无法从android接收php中的post参数?

java - 从数据库中计算产品时 Firebase 变慢 (Android)

java - 如何通过选择微调器来启用或禁用 editText

android - 禁用按钮,编辑文本

Android - 动态添加按钮 - 出现错误

Android 8.0 Oreo 在聚焦 TextInputEditText 时崩溃

java - Android EditText 有固定的输入

java - 父View折叠时如何清除子ViewHolder焦点?

java - java [android] 中小数点后 3 位的 EditText 掩码

安卓内存泄漏?位图超出 MapView 的 VM 预算错误