Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My program crashes after clicking add or sub button

What I have tried:

Java
package com.yoursrishabh.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;

public class MainActivity extends AppCompatActivity
{

    EditText first,second;
    Button addB,subB,mulB,divB;
    TextView res;

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

        first = (EditText)findViewById(R.id.tf1);
        second = (EditText)findViewById(R.id.tf2);
        addB = (Button)findViewById(R.id.btnAdd);
        subB = (Button)findViewById(R.id.btnSub);
        mulB = (Button)findViewById(R.id.btnMul);
        divB = (Button)findViewById(R.id.btnDiv);
        res = (TextView)findViewById(R.id.tvRes);

        addB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                int f = Integer.parseInt(first.getText().toString());
                int s = Integer.parseInt(second.getText().toString());
                int sum = f+s;
                res.setText(sum);
            }
        });

        subB.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                int f = Integer.parseInt(first.getText().toString());
                int s = Integer.parseInt(second.getText().toString());
                int sub = f-s;
                res.setText(sub);
            }
        });


    }
}


XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    tools:context=".MainActivity"
    tools:visibility="visible">

    <EditText
        android:id="@+id/pt1"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginTop="30dp"
        android:autofillHints=""
        android:ems="10"
        android:inputType="textPersonName"
        android:text="First no."
        android:textColor="#E32D2D"
        android:textSize="22sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/pt2"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginTop="20dp"
        android:autofillHints=""
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Second no."
        android:textAlignment="viewStart"
        android:textColor="#E32D2D"
        android:textSize="22sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pt1" />

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/tf1"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="70dp"
        android:layout_marginTop="36dp"
        android:hint="Enter no."
        android:visibility="visible"
        app:layout_constraintStart_toEndOf="@+id/pt1"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/tf2"
        android:layout_width="120dp"
        android:layout_height="53dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="20dp"
        android:hint="Enter no."
        app:layout_constraintStart_toEndOf="@+id/pt2"
        app:layout_constraintTop_toBottomOf="@+id/tf1" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="77dp"
        android:layout_height="58dp"
        android:layout_marginStart="49dp"
        android:layout_marginTop="72dp"
        android:background="#CDDC39"
        android:text="Add"
        android:textColor="#FFFFFF"
        android:textSize="17sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pt2" />

    <TextView
        android:id="@+id/tvRes"
        android:layout_width="120dp"
        android:layout_height="30dp"
        android:layout_marginStart="48dp"
        android:layout_marginTop="304dp"
        android:text="Answer:"
        android:textColor="#F44336"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnAdd" />

    <Button
        android:id="@+id/btnSub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:layout_marginEnd="118dp"
        android:text="Sub"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tf2" />

    <Button
        android:id="@+id/btnMul"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginEnd="111dp"
        android:layout_marginBottom="160dp"
        android:text="Mult"
        app:layout_constraintBottom_toTopOf="@+id/tvRes"
        app:layout_constraintEnd_toStartOf="@+id/btnDiv"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btnDiv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="88dp"
        android:layout_marginBottom="305dp"
        android:text="Div"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Posted
Updated 10-Jan-21 0:56am
Comments
Patrice T 10-Jan-21 6:34am    
And you have an error message ?
yoursRishabh 10-Jan-21 6:38am    
Actually I am a beginner to android and unable to find the actual error message, I will be thankful if you guide me for the same.
Patrice T 10-Jan-21 6:40am    
Sorry, I am not Android user.
David Crow 10-Jan-21 11:38am    
Are the first and second variables non-null? Should you be calling setText() with a non-String value?

1 solution

Java
int f = Integer.parseInt(first.getText().toString());
int s = Integer.parseInt(second.getText().toString());
int sum = f+s;
res.setText(sum);

You make the classic mistake in your code that you assume all the input data is valid. If there is anything wrong with the text in either first or second, then your code will crash. You need to capture the text first (and you do not need to call toString on an object that returns a string!), and check that it contains a valid number. Once you have done that you can call parseInt.

However, the actual exception in this case may be something else. So you need to provide the full details of the exception message, including the line number that it occurs on.
 
Share this answer
 
Comments
yoursRishabh 10-Jan-21 7:03am    
https://www.youtube.com/watch?v=59vw7NT0kOg&list=PLsyeobzWxl7p-lZvWabkVJdM_UVURhUh4&index=12
Richard MacCutchan 10-Jan-21 7:13am    
I have no interest in watching some YouTube video. If you want help with your problem, then either provide the information that we have requested, or ask the person who created the video.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900