0

My app crashes after button click. The app I was making was for tables. But unfortunately the app crashes after the button click

package com.example.ch1practisemulti;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button button;
    EditText editText;
    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;

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

        button = findViewById(R.id.button);
        editText = findViewById(R.id.editTextNumber);
        text1 = findViewById(R.id.text1);
        text2 = findViewById(R.id.text2);
        text3 = findViewById(R.id.text3);
        text4 = findViewById(R.id.text4);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int t1 = Integer.parseInt(editText.getText().toString());
                int t2 = Integer.parseInt(editText.getText().toString()) * 2;
                int t3 = Integer.parseInt(editText.getText().toString()) * 3;
                int t4 = Integer.parseInt(editText.getText().toString()) * 4;
                text1.setText(+t1);
                text2.setText(+t2);
                text3.setText(+t3);
                text4.setText(+t4);
            }
        });
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Why is there a `+` symbol in every `setText()` method? You should also post your crash log. – Praveen Apr 30 '22 at 15:00
  • concatenation operator sir – Tejpreet Singh Apr 30 '22 at 15:01
  • 1
    What are you concatenating it with, you've simply put `+` in front of an `int`. – Praveen Apr 30 '22 at 15:06
  • Most likely crash is a NumberFormatException if the text values are blank or not integers when you call `parseInt`, but you need to post the actual crash log. See [this for details too](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Tyler V Apr 30 '22 at 15:32

1 Answers1

0

If you send an int/Integer to TextView, it will try to search resources for a string with that id(in res/values/strings.xml). See setText.

You need to cast it back to a string again, e:g: String.valueOf(t4) or by t4+""

Abdullah Z Khan
  • 1,272
  • 1
  • 8
  • 18