简体   繁体   English

Android Eclipse EditText崩溃

[英]Android Eclipse EditText crash

I am trying to make Cumulative GPA application. 我正在尝试累积GPA应用程序。 It contains 7 editTexts and one textView . 它包含7个editTexts和一个textView One of them is for the number of the courses and the others are for the courses GPA. 其中一项用于课程数量,其他用于课程GPA。 When I use it as for 6 courses it works but when I use it as example for 3 courses that I have to keep 3 of them empty the application crash. 当我将其用作6门课程时,它可以工作,但是当我将其用作3门课程为例时,则必须让其中3门为空,否则应用程序崩溃。

public class MainActivity extends Activity {

    EditText e1, e2, e3, e4, e5, e6, e7;
    TextView t1;
    Button b1, b2;
    double a=0;
    double b=0;
    double c=0;
    double d=0;
    double e=0;
    double f=0;
    double g=0;
    double h=0;


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

        e1=(EditText)findViewById(R.id.editText1);
        e2=(EditText)findViewById(R.id.editText2);
        e3=(EditText)findViewById(R.id.editText3);
        e4=(EditText)findViewById(R.id.editText4);
        e5=(EditText)findViewById(R.id.editText5);
        e6=(EditText)findViewById(R.id.editText6);
        e7=(EditText)findViewById(R.id.editText7);

        t1=(TextView)findViewById(R.id.textView3);
        b2=(Button)findViewById(R.id.button2);

        b2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            a=Double.parseDouble(e1.getText().toString());
            b=Double.parseDouble(e2.getText().toString());
            c=Double.parseDouble(e3.getText().toString());
            d=Double.parseDouble(e4.getText().toString());
            e=Double.parseDouble(e5.getText().toString());
            f=Double.parseDouble(e6.getText().toString());
            g=Double.parseDouble(e7.getText().toString());



            h= ((a+b+c+d+e+f)/g);


            t1.setText(Double.toString(h));

I tried this code: 我尝试了这段代码:

{
    if (g==3)
    e4.setText("0");
    e5.setText("0");
    e6.setText("0");
}

but the same thing happened. 但同样的事情发生了。 Notice : e7 = the number of the courses 注意:e7 =课程数

when you are keeping any one of edittext is empty your code will crash for eg: If your editext e1 is empty follwing code wont work as getText().toString() dont have any data 当您保持edittext的任何一个为空时,您的代码将崩溃,例如:如果editext e1为空,则下面的代码将无法正常工作,因为getText()。toString()没有任何数据

a=Double.parseDouble(e1.getText().toString());

you have to do like 你必须喜欢

if(!e1.getText().toString().equals(""))
{
a=Double.parseDouble(e1.getText().toString());
}

as for all..... 至于所有.....

Modify your getText lines like this: 像这样修改您的getText行:

String e1 = e1.getText().toString();
a= e1.equals("")?0:Double.parseDouble(e1);

That will prevent the NumberFormatException that you must be getting now. 这将防止您现在必须获取NumberFormatException

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

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