NumberFormatException:empty String; Double.parseDouble

时间:2017-02-11 04:16:51

标签: java android numberformatexception

我的应用程序一直崩溃,并给了我一个:

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

}

1 个答案:

答案 0 :(得分:0)

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

所有的EditTexts都是空的。你不能getText() onCreate 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

注意:当user时,您将获得零除错误