简体   繁体   English

如果按下按钮时 edittext 部分为空白,我想显示 toast 错误消息 (Android Studio)

[英]I want to show a toast error msg if the edittext section is blank when the button is pressed (Android Studio)

I made a simple calculator app in android studio but the app crashes when no value is entered in the edittext section.我在 android studio 制作了一个简单的计算器应用程序,但是当没有在编辑文本部分输入任何值时应用程序崩溃。 I want to show a toast msg when no value is entered in the edittext section.当没有在 edittext 部分输入任何值时,我想显示祝酒词。 Please check this issue and guide me how to fix this and add toast.请检查此问题并指导我如何解决此问题并添加吐司。 here n1 and n2 is the id of edittext.这里的 n1 和 n2 是 edittext 的 id。

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();

        }
    });

}

} }

Do the same in all the other OnClickListner在所有其他 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();

                }
            }
        });

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.理想情况下,您希望用 try{}..catch{} 块包围 ParseInt() 并处理那里的任何错误以阻止应用程序崩溃。

  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.发生崩溃是因为您没有任何 null 支票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM