Home > Mobile >  App Crashing as soon as i hit the add button
App Crashing as soon as i hit the add button

Time:11-17

Hi guys i am new to android and wanted make a calculator but i guess i might have messed up the onClicklistner function because whenever i hit the ' ' button the app crashes. I am just making a simple calculator for now but the add doesnt work so didnt go forward. Any ideas how i can solve this issue.

package com.example.calculator;

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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button add;
    private Button sub;
    private Button mul;
    private Button del;
    private EditText num1;
    private EditText num2;
    private TextView textView;

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

        add = findViewById(R.id.button7);
        sub = findViewById(R.id.button8);
        mul = findViewById(R.id.button11);
        del = findViewById(R.id.button12);
        num1 = findViewById(R.id.editTextNumber);
        num2 = findViewById(R.id.editTextNumber2);
        textView = findViewById(R.id.textView2);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String n1 = num1.getText().toString();
                Integer number1 = Integer.parseInt(n1);
                String n2= num2.getText().toString();
                Integer number2 = Integer.parseInt(n2);

                Integer result = number1   number2;
                textView.setText(result);
            }
        });
    }
}

Here is the xml if you need ==>

<Button
    android:id="@ id/button7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" "
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@ id/button8"
    app:layout_constraintHorizontal_bias="0.143"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.499" />


CodePudding user response:

   Integer result = number1   number2;
   textView.setText(result);

You are using an integer to set text on textview so currently its using that int as an resource id as an overriden method . instead you need to use the below code and it will work

 Integer result = number1   number2;
 textView.setText(result "");

CodePudding user response:

Use the

  String n1 = num1.getText().toString();
                Integer number1 = Integer.parseInt(n1);
                String n2= num2.getText().toString();
                Integer number2 = Integer.parseInt(n2);
                Integer result = number1   number2;
                textView.setText(String.Valueof(result));

  • Related