java.lang.StringIndexOutOfBoundsException:Android

时间:2017-07-29 11:53:44

标签: java android indexoutofboundsexception

当我使用虚拟仿真器测试我的应用程序时(没有我可以测试的有效设备),我发现了一个错误。那就是 java.lang.StringIndexOutOfBoundsException:length = 0; index = 2 错误,当我点击EditText并尝试写入时弹出。 EditText设置为在单击另一个按钮时显示。这是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.luca.cecco.activity.allergie"
android:padding="20dp"
android:orientation="vertical"
android:background="@drawable/possible4_bg">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hai allergie?"
        android:fontFamily="sans-serif"
        android:textAllCaps="true"
        style="@style/BOLD"
        android:textAlignment="center"
        android:layout_gravity="center"
        android:layout_marginTop="200dp"
        android:background="@drawable/round_shape_btn"
        android:textColor="@color/blank"
        android:padding="10dp"
        android:textSize="40sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="105dp">

        <Button
            android:id="@+id/btn_allergia_si"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="120dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/round_shape_clicked_btn"
            android:fontFamily="sans-serif"
            android:text="Sì"
            android:textAllCaps="true"
            style="@style/BOLD"
            android:gravity="center"
            android:textColor="@color/blank"
            android:textSize="30sp" />

        <Button android:id="@+id/btn_allergia_no"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="No"
            style="@style/BOLD"
            android:gravity="center"
            android:background="@drawable/round_shape_clicked_btn"
            android:fontFamily="sans-serif"
            android:textAllCaps="true"
            android:textColor="@color/blank"
            android:textSize="30sp"
            android:layout_marginLeft="110dp"/>

    </LinearLayout>

    <EditText android:id="@+id/allergia_si"
        android:layout_width="285dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="150dp"
        android:hint="Inserisci qui le tue allergie"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textColorHint="@color/blank"
        android:background="@drawable/round_shape_clicked_btn"
        android:textAllCaps="true"
        android:padding="5dp"/>

    <Button android:id="@+id/btn_avanti3"
        android:layout_marginTop="100dp"
        android:layout_marginRight="30dp"
        android:layout_width="110dp"
        android:textColor="@color/blank"
        android:layout_height="85dp"
        android:text="Avanti"
        android:gravity="center"
        android:textAlignment="center"
        android:textSize="20sp"
        android:layout_gravity="end"
        style="@style/AlertDialog.AppCompat"
        android:background="@color/blue_button"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:fontFamily="sans-serif"
    />

    </LinearLayout>

</RelativeLayout>

这是Java类:

private Button btn_si;
private Button btn_no;
private EditText et_allergia;
private Button btn_avanti;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_allergie);

    btn_si = (Button) findViewById(R.id.btn_allergia_si);
    btn_no = (Button) findViewById(R.id.btn_allergia_no);
    et_allergia = (EditText) findViewById(R.id.allergia_si);
    btn_avanti = (Button) findViewById(R.id.btn_avanti3);

    et_allergia.setVisibility(View.INVISIBLE);
    btn_no.setOnClickListener(new View.OnClickListener() {
       @Override
        public void onClick(View view)
       {
           btn_no.setBackground(getResources().getDrawable(R.drawable.round_shape_clicked_btn)); //cambia il colore di base
       }
    });

    btn_si.setOnClickListener(new View.OnClickListener() { //cambia il colore di base e rende visibile l'edit text
        @Override
        public void onClick(View view)
        {
            btn_si.setBackground(getResources().getDrawable(R.drawable.round_shape_clicked_btn)); //cambia il colore di base
            et_allergia.setVisibility(View.VISIBLE);
        }
    });

    btn_avanti.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            Intent intent = new Intent(allergie.this, Preferenze.class);
            startActivity(intent);
        }
    });

}

1 个答案:

答案 0 :(得分:0)

您收到错误因为,您已设置属性&#39; textAllCaps&#39;在EditText中。此属性主要用于TextView。 删除此属性,然后重试。