关闭应用程序后单击按钮后出现致命错误

Getting a FATAL error after a click of a button with the closing of the app

我正在创建一个从一种货币到另一种货币的转换器,反之亦然。下面的方法是 运行 只需点击一个按钮,但它会报错并退出应用程序。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/LBP"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number"
    android:minHeight="48dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.497"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.367"
    tools:ignore="SpeakableTextPresentCheck" />

<EditText
    android:id="@+id/Dollar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number"
    android:minHeight="48dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.497"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.542"
    tools:ignore="SpeakableTextPresentCheck" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="converter"
    android:text="Convert"
    android:textSize="20sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.672" />

<Spinner
    android:id="@+id/spinner"
    android:layout_width="222dp"
    android:layout_height="48dp"
    android:spinnerMode="dropdown"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.497"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.241"
    tools:ignore="SpeakableTextPresentCheck" />

<TextView
    android:id="@+id/textView"
    android:layout_width="50dp"
    android:layout_height="27dp"
    android:labelFor="@id/LBP"
    android:text="LBP"
    android:textAlignment="center"
    android:textSize="20sp"
    app:flow_horizontalAlign="center"
    app:flow_verticalAlign="center"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.091"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.37" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="54dp"
    android:layout_height="27dp"
    android:labelFor="@id/Dollar"
    android:text="$"
    android:textAlignment="center"
    android:textSize="20sp"
    app:flow_horizontalAlign="center"
    app:flow_verticalAlign="center"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.092"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.538" />

  </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:

package com.example.converter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements 
AdapterView.OnItemSelectedListener {

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

    Spinner spinner = findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = 
ArrayAdapter.createFromResource(this, R.array.currencies, 
android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int 
i, long l) {
    EditText LBP = findViewById(R.id.LBP);
    EditText Dollar = findViewById(R.id.Dollar);
    String text = adapterView.getItemAtPosition(i).toString();
    if (text.equals("LBP")) {
        LBP.setEnabled(false);
        Dollar.setEnabled(true);
    } else if (text.equals("DOLLAR")) {
        LBP.setEnabled(true);
        Dollar.setEnabled(false);
    }

}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

public void converter(View view) {
    int rate = 15000;
    EditText LBP = (EditText) findViewById(R.id.LBP);
    EditText Dollar = (EditText) findViewById(R.id.Dollar);
    if (LBP.isEnabled()) {
        int LBPAmount = Integer.parseInt(LBP.getText().toString());
        double DollarResult = LBPAmount / rate;
        Dollar.setText((int) DollarResult);
    } else if (Dollar.isEnabled()) {
        int DollarAmount = 
        Integer.parseInt(Dollar.getText().toString());
        double LBPResult = DollarAmount * rate;
        LBP.setText((int) LBPResult);
      }
    }
  }

这是错误:

2021-09-11 10:51:28.996 20809-20809/com.example.converter E/ample.converte: Invalid ID 
0x00003a98.
2021-09-11 10:51:28.996 20809-20809/com.example.converter D/AndroidRuntime: Shutting down VM
2021-09-11 10:51:28.998 20809-20809/com.example.converter E/AndroidRuntime: FATAL EXCEPTION: 
main
Process: com.example.converter, PID: 20809
java.lang.IllegalStateException: Could not execute method for android:onClick
     at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
  at android.view.View.performClick(View.java:6597)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access00(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
    at android.view.View.performClick(View.java:6597) 
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
    at android.view.View.performClickInternal(View.java:6574) 
    at android.view.View.access00(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25885) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
 Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x3a98
    at android.content.res.Resources.getText(Resources.java:348)
    at android.widget.TextView.setText(TextView.java:5831)
    at com.example.converter.MainActivity.converter(MainActivity.java:59)
    at java.lang.reflect.Method.invoke(Native Method) 
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
    at android.view.View.performClick(View.java:6597) 
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
    at android.view.View.performClickInternal(View.java:6574) 
    at android.view.View.access00(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25885) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我认为是因为Integer.parseInt,但我不认为它写错了。有人知道吗?

错误在onClick函数,即您的应用程序的转换器函数。您试图以 Int 的形式将值设置为 EditText, Dollar.setText((int) DollarResult) 这不是这样做的适当方式。您需要将 String 设置为 EditText 。 所以你的转换器函数应该是这样的:


    public void converter(View view) {
        int rate = 15000;
        EditText LBP = (EditText) findViewById(R.id.LBP);
        EditText Dollar = (EditText) findViewById(R.id.Dollar);
        if (LBP.isEnabled()) {
            int LBPAmount = Integer.parseInt(LBP.getText().toString());
            double DollarResult = LBPAmount / rate;
            Dollar.setText(String.valueOf( (int) DollarResult));
        } else if (Dollar.isEnabled()) {
            int DollarAmount =
                    Integer.parseInt(Dollar.getText().toString());
            double LBPResult = DollarAmount * rate;
            LBP.setText(String.valueOf((int) LBPResult));
        }
    }

上面执行的更改是将 Dollar.setText((int) DollarResult) 替换为 Dollar.setText(String.valueOf( (int) DollarResult));,同样的更改已应用于 LBP。