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

Time:10-12

What I'm doing is, "ask" for a number in kilometers, and once you click the button to convert that number to centimeters and show the result in the TextView, but when you click the button, the App closes.

I'm using a Samsung Galaxy A70

Android version: 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" />

This is the Error it shows on Logcat tab:

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.access$3600(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.access$3600(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.access$3600(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:11002021-10-11 17:20:20.010 12602-12602/com.example.conversor I/Process: Sending signal. PID: 12602 SIG: 9

CodePudding user response:

You are passing an int as your argument when you do

tv.setText(valor);

You need to give it a ressource ID, or a String. You can convert your int to a string using

tv.setText(String.valueOf(valor));

CodePudding user response:

more simpler is to just concatenate it with empty string.

                  tv.setText(valor   "");
  • Related