Android - EditText在我写东西时给出了IndexOutOfBounds Exception

时间:2016-02-22 15:05:03

标签: android android-edittext

我正在尝试使用一个评级字段和一个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();
    }
}

有没有人有类似的问题?或者有人知道可能会发生什么吗?

1 个答案:

答案 0 :(得分:8)

请在您的edittext视图中添加以下代码

android:inputType="text" 

android:inputType="textMultiLine"