Home > front end >  I want to show a toast error msg if the edittext section is blank when the button is pressed (Androi
I want to show a toast error msg if the edittext section is blank when the button is pressed (Androi

Time:03-21

I made a simple calculator app in android studio but the app crashes when no value is entered in the edittext section. I want to show a toast msg when no value is entered in the edittext section. Please check this issue and guide me how to fix this and add toast. here n1 and n2 is the id of edittext.

MainActivity (code):

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

    n1 = findViewById(R.id.n1);
    n2 = findViewById(R.id.n2);
    add = findViewById(R.id.add);
    sub = findViewById(R.id.sub);
    multi = findViewById(R.id.multi);
    div = findViewById(R.id.div);
    tv = findViewById(R.id.tv);

    // addition part

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int p,q,r;
            p = Integer.parseInt(n1.getText().toString());
            q = Integer.parseInt(n2.getText().toString());
            r = p q;
            tv.setText("Result is "   r);
            Toast.makeText(MainActivity.this, "Successfully Calculated", Toast.LENGTH_SHORT).show();

        }
    });
    //subtraction part

    sub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int p,q,r;
            p = Integer.parseInt(n1.getText().toString());
            q = Integer.parseInt(n2.getText().toString());
            r = p-q;
            tv.setText("Result is "   r);
            Toast.makeText(MainActivity.this, "Successfully Calculated", Toast.LENGTH_SHORT).show();

        }
    });

    // multiplication part

    multi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int p,q,r;
            p = Integer.parseInt(n1.getText().toString());
            q = Integer.parseInt(n2.getText().toString());
            r = p*q;
            tv.setText("Result is "   r);
            Toast.makeText(MainActivity.this, "Successfully Calculated", Toast.LENGTH_SHORT).show();

        }
    });

    //divison part

    div.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int p,q,r;
            p = Integer.parseInt(n1.getText().toString());
            q = Integer.parseInt(n2.getText().toString());
            r = p/q;
            tv.setText("Result is "   r);
            Toast.makeText(MainActivity.this, "Successfully Calculated", Toast.LENGTH_SHORT).show();

        }
    });

}

}

CodePudding user response:

Do the same in all the other OnClickListner

add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int p,q,r;
                if(!n1.getText().toString().equals("") && !n2.getText().toString().equals("")) {
                    p = Integer.parseInt(n1.getText().toString());
                    q = Integer.parseInt(n2.getText().toString());
                    r = (p   q);
                    Toast.makeText(MainActivity.this, "Successfully Calculated", Toast.LENGTH_SHORT).show();
                    tv.setText("Result is "   r);
                }else {
                    Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();

                }
            }
        });

CodePudding user response:

You could do a preliminary check to see if any text was entered like

if(n1.getText()) ....

Ideally you'd want to surround the ParseInt() with a try{}..catch{} block and handle any errors there to stop the app from crashing.

CodePudding user response:

  int n1Value = n1.getText().toString();
  int n2Value = n2.getText().toString();
  p = n1Value !=null&&!n1Value.isEmpty()? Integer.parseInt(n1Value):0 
  p = n2Value !=null&&!n2Value.isEmpty()? Integer.parseInt(n2Value):0 

Add this in your click listeners. the crash is happening because you don't have any null check.

  • Related