簡體   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