0

Did absolutely nothing but the app is crashing. The app is running when I did not include the conversion part. XMl :

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <EditText
        android:id="@+id/numberInput"
        android:layout_width="63dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:singleLine="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

MainActivity.java :

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EditText numberInput = (EditText) findViewById(R.id.numberInput);
    String s = numberInput.getText().toString();
    int n = Integer.parseInt(s);

} }

1 Answers1

0

Try to check string before casting and return 0 if string is empty.

int n = s.isEmpty()?0:Integer.parseInt(s);
Zeon
  • 535
  • 8
  • 23