繁体   English   中英

NumberFormatException: 空字符串; Double.parseDouble

[英]NumberFormatException: empty String ; Double.parseDouble

我的应用程序不断崩溃并给我一个:

 java.lang.NumberFormatException: empty String

还说这个

 FATAL EXCEPTION: main
              Process: com.example.ayyan.jellybeanestimator, PID: 2966
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ayyan.jellybeanestimator/com.example.ayyan.jellybeanestimator.MainActivity}: java.lang.NumberFormatException: empty String

 package com.example.ayyan.jellybeanestimator;

 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.widget.TextView;
 import android.widget.EditText;
import android.widget.Button;
import android.view.View;

public class MainActivity extends AppCompatActivity {
EditText jellyBeanLength, jellyBeanDiameter, jarSizeVolume;
double jellybeantall, jellybeanfat, jellybeanspace, volumeOfOneJellyBean, volumeofBeans;
final double loadFactor = .698;
TextView answer;


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


    jellyBeanLength = (EditText) findViewById (R.id.length);
    jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
    jarSizeVolume = (EditText) findViewById (R.id.jarsize);
    jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
    jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
    jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
    answer = (TextView) findViewById (R.id.answer);
    Button calculate = (Button) findViewById (R.id.calculate);
    calculate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            volumeOfOneJellyBean =  ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
            volumeofBeans = (jellybeanspace*loadFactor)/volumeOfOneJellyBean;
            int jellyguess = (int) (volumeofBeans);
            answer.setText("You have this amount of beans in your jar" + jellyguess);
        }
    });
}

}
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());

你所有的 EditTexts 开始都是空的。 你不能在onCreate getText() ,你必须在onClick做到

calculate.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
        jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
        jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());

        volumeOfOneJellyBean =  ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
        volumeofBeans = (jellybeanspace*loadFactor)/volumeOfOneJellyBean;
        int jellyguess = (int) (volumeofBeans);
        answer.setText("You have this amount of beans in your jar" + jellyguess);
    }
});

注意:当volumeOfOneJellyBean == 0 ,您将得到除以零错误

暂无
暂无

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

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