Parse Int Сбои моей программы, и да, у меня установлен тип xml iput на число - PullRequest
0 голосов
/ 29 декабря 2018

Когда я запускаю свое приложение, все работает нормально, пока я не нажму кнопку, которая запускает метод Calculate (), и parse int не завершит работу программы.

Это упрощенная версия моей проблемы.Когда я нажимаю кнопку «Рассчитать», запускается метод Calculate ().Тем не менее, он падает.Ошибка не выдается, он просто падает в эмуляторе.

КОД JAVA ДЛЯ ОСНОВНОЙ ДЕЯТЕЛЬНОСТИ:

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


}

public void calculate(View view) {
    EditText length_find = findViewById(R.id.feet);
    EditText pounds_find = findViewById(R.id.pounds);
    int length = Integer.parseInt(length_find.getText().toString());
    int pounds = Integer.parseInt(pounds_find.getText().toString());

    int sum = length + pounds;

    Toast.makeText(MainActivity.this, sum, LENGTH_LONG).show();

}
}

КОД XML:

<Button
    android:id="@+id/calculate"
    android:layout_width="158dp"
    android:layout_height="70dp"
    android:layout_marginStart="113dp"
    android:layout_marginEnd="113dp"
    android:layout_marginBottom="73dp"
    android:onClick="calculate"
    android:text="Calculate"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<EditText
    android:id="@+id/pounds"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="83dp"
    android:layout_marginTop="90dp"
    android:layout_marginEnd="86dp"
    android:layout_marginBottom="24dp"
    android:ems="10"
    android:hint="#'s"
    android:inputType="number"
    app:layout_constraintBottom_toTopOf="@+id/feet"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/feet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="83dp"
    android:layout_marginTop="23dp"
    android:layout_marginEnd="86dp"
    android:ems="10"
    android:hint="Feet"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/pounds" />

1 Ответ

0 голосов
/ 29 декабря 2018

Чтобы проверить, что происходит с непредвиденным исключением, вы можете сделать следующее:

 public void calculate(View view) {
     EditText length_find = findViewById(R.id.feet);
     EditText pounds_find = findViewById(R.id.pounds);

     try {
         int length = Integer.parseInt(length_find.getText().toString());
         int pounds = Integer.parseInt(pounds_find.getText().toString());

         int sum = length + pounds;
         Toast.makeText(MainActivity.this, sum, LENGTH_LONG).show();
     catch(Exception ex) {
         Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
     }

 }
...