应用程序在空时崩溃

时间:2017-03-07 12:15:34

标签: java android

我最近开始学习android studio,我做了一个应用程序,显示数字是三角形还是方形。问题是当用户没有将任何内容放入文本字​​段时,我的应用程序崩溃了!为什么会这样?我需要改变什么?

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    class Number {

        int number;

        public boolean isSquare() {

            double squareRoot = Math.sqrt(number);

            if (squareRoot == Math.floor(squareRoot)) {

                return true;

            } else {

                return false;

            }


        }

        public boolean isTriangular() {

            int x = 1;

            int y = 1;

            while (y < number) {

                x++;

                y = y + x;

            }

            if ( y == number ) {

                return true;

            } else {

                return false;

            }



        }


    }

    public void testNumber(View view) {

        String msg = "";

        EditText usersNumber = (EditText) findViewById(R.id.usersNumber);

        Number myNumber = new Number();

        myNumber.number = Integer.parseInt(usersNumber.getText().toString());


        if (usersNumber.getText().toString().isEmpty()) {


            msg = myNumber.number + "Please eneter a Number!";

        } else {


            if (myNumber.isSquare()) {

                if (myNumber.isTriangular()) {

                    msg = myNumber.number + "is both square and triangular!";
                } else {

                    msg = myNumber.number + "is square but not traingular";


                }


            } else {


                if (myNumber.isTriangular()) {

                    msg = myNumber.number + "is triangular but not suqare !";
                } else {

                    msg = myNumber.number + "is neither";
                }


            }




        }

        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();

    }


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

2 个答案:

答案 0 :(得分:2)

如果用户未在edittext文件中输入数字,usersNumber.getText().toString()将返回空白,无法将其作为数字。

这就是您在此声明中获得异常的原因:Integer.parseInt(usersNumber.getText().toString());

有关详细信息,请参阅here。 要阻止您的应用崩溃,您可以在代码周围使用try { ... } catch {...}块:

try {
 myNumber.number = Integer.parseInt(usersNumber.getText().toString());


        if (usersNumber.getText().toString().isEmpty()) {


            msg = myNumber.number + "Please eneter a Number!";

        } else {


            if (myNumber.isSquare()) {

                if (myNumber.isTriangular()) {

                    msg = myNumber.number + "is both square and triangular!";
                } else {

                    msg = myNumber.number + "is square but not traingular";


                }


            } else {


                if (myNumber.isTriangular()) {

                    msg = myNumber.number + "is triangular but not suqare !";
                } else {

                    msg = myNumber.number + "is neither";
                }


            }




        }
}catch (NumberFormatException ne){
  //handle the exception
}

或者在将其解析为整数之前检查null:

 if (!usersNumber.getText().toString().equals("")){
 myNumber.number = Integer.parseInt(usersNumber.getText().toString());
} else {
  myNumber.number = 0;
}

答案 1 :(得分:-1)

将您想要的代码放入try catch语句中,这样,如果发生错误,应用程序不会崩溃。

try
{
  //Insert code here//
}
catch(Exception e)
{
  //Handle error here//
}