Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - EditText gives IndexOutOfBounds Exception when I write something

I am trying to do a simple fragment with one rating field and one EditText for comments. The problem occours when I click on the EditText Field and type something. At this moment, the app crashes.

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>

The Log error:

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)

The Java class

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();
    }
}

Has anyone had a similar problem? Or does anyone have an idea of what might be happening?

like image 763
Alfredo Del Fabro Neto Avatar asked Feb 08 '23 16:02

Alfredo Del Fabro Neto


1 Answers

Please add below code in your edittext view

android:inputType="text" 

or

android:inputType="textMultiLine"
like image 69
Debashis Bose Avatar answered Feb 11 '23 01:02

Debashis Bose