My App crashes after button click can some please help the app I was making was for tables. But unfortunately the app crashes after the button click strong text
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);
}
});
}
}
CodePudding user response:
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 ""