我正在尝试将一串数字放入 TextView 但我的应用程序在显示之前崩溃了

I'm trying to put a string of numbers into a TextView but my App crashes before showing it

我正在做的是,“询问”一个以公里为单位的数字,一旦你点击按钮将该数字转换为厘米并在 TextView 中显示结果,但是当你点击按钮时,应用程序关闭。

我使用的是三星 Galaxy A70

Android版本:10

MainActivity.java

public void calcular(View v){
        EditText text = findViewById(R.id.pt_entrada);
        input = Integer.parseInt(text.getText().toString());
        valor = input * cm;
        Log.d("info", String.valueOf(valor)); // This actually gives me de log on the Logcat tab.
        TextView tv = (TextView)findViewById(R.id.tv_pantalla); // But once the app arrives here, it crashes.
        tv.setText(valor);
    }

activity_main.xml

<Button
        android:id="@+id/b_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:onClick="calcular"
        android:text="@string/calcular"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/pt_entrada"
        app:layout_constraintTop_toTopOf="@+id/pt_entrada" />

    <TextView
        android:id="@+id/tv_pantalla"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pt_entrada" />

    <EditText
        android:id="@+id/pt_entrada"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        android:autofillHints="@string/kmacm"
        android:ems="10"
        android:hint="@string/kmacm"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toStartOf="@+id/b_send"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

这是 Logcat 选项卡上显示的错误:

Process: com.example.conversor, PID: 12602
    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:7862)
        at android.widget.TextView.performClick(TextView.java:15004)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7831)
        at android.view.View.access00(View.java:879)
        at android.view.View$PerformClick.run(View.java:29359)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8167)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     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:7862) 
        at android.widget.TextView.performClick(TextView.java:15004) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
        at android.view.View.performClickInternal(View.java:7831) 
        at android.view.View.access00(View.java:879) 
        at android.view.View$PerformClick.run(View.java:29359) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:8167) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100) 
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x64
        at android.content.res.Resources.getText(Resources.java:402)
        at android.widget.TextView.setText(TextView.java:6942)
        at com.example.conversor.MainActivity.calcular(MainActivity.java:29)
        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:7862) 
        at android.widget.TextView.performClick(TextView.java:15004) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
        at android.view.View.performClickInternal(View.java:7831) 
        at android.view.View.access00(View.java:879) 
        at android.view.View$PerformClick.run(View.java:29359) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:8167) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100) 
2021-10-11 17:20:20.010 12602-12602/com.example.conversor I/Process: Sending signal. PID: 12602 SIG: 9

当你这样做时,你正在传递一个 int 作为你的参数

tv.setText(valor);

您需要给它一个资源 ID,或者一个字符串。您可以使用

将 int 转换为字符串
tv.setText(String.valueOf(valor));

更简单的方法是将它与空字符串连接起来。

                  tv.setText(valor + "");