java.lang.NumberFormatException:无效的双精度:“”在 java.lang.StringToReal.invalidReal
Posted
技术标签:
【中文标题】java.lang.NumberFormatException:无效的双精度:“”在 java.lang.StringToReal.invalidReal【英文标题】:java.lang.NumberFormatException: Invalid double: "" at java.lang.StringToReal.invalidReal 【发布时间】:2016-03-16 09:02:36 【问题描述】:您好,我正在从 edittext 中获取一个值并转换为在其他文本中显示该值,但出现运行时错误 java.lang.NumberFormatException: Invalid double: "".at java.lang.StringToReal.invalidReal。在我的 setAdapter 中,它只采用 franheit,这意味着只需要详细信息。我不明白错误在哪里
public class UniqueConverter extends Fragment
private Spinner fromDetails, toDetails;
private EditText fromInput, toInput;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_uniquelayout, container, false);
onInitVies(view);
int position = getArguments().getInt("position");
String ifrom,rto;
Double inp;
double out = 0;
switch (position)
case 0:
setAdapter(temperatureform, "Celsius", "Fahrenheit");
ifrom = (String) fromDetails.getSelectedItem();
rto = (String) toDetails.getSelectedItem();
inp = Double.valueOf(fromInput.getText().toString());
TemperatureConverter con = new TemperatureConverter();
TemperatureConverter.Units fromUnit = TemperatureConverter.Units.fromString(ifrom);
TemperatureConverter.Units toUnit = TemperatureConverter.Units.fromString(rto);
out = con.TemperatureConvert(fromUnit,toUnit,inp);
break;
case 1:
setAdapter(weightform, "Kilograms", "Grams");
ifrom = (String) fromDetails.getSelectedItem();
rto = (String) toDetails.getSelectedItem();
inp = Double.valueOf(fromInput.getText().toString());
WeightConverter converter = new WeightConverter();
WeightConverter.Inus fromInus = WeightConverter.Inus.fromString(ifrom);
WeightConverter.Inus toInus = WeightConverter.Inus.fromString(rto);
out = converter.WeightConvert(fromInus, toInus, inp);
break;
final double finalOut = out;
fromInput.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
@Override
public void afterTextChanged(Editable s)
if (!fromInput.getText().toString().equals(""))
toInput.setText(String.valueOf(finalOut));
else
toInput.setText("");
);
return view;
private void setAdapter(String[] spinnerItems, String fromDefaultText, String toDefaultText)
fromDetails.setAdapter(new CustomSpinnerAdapter(getContext(), R.layout.spinner_item, spinnerItems, fromDefaultText));
toDetails.setAdapter(new CustomSpinnerAdapter(getContext(), R.layout.spinner_item, spinnerItems, toDefaultText));
private void onInitVies(View view)
fromDetails = (Spinner) view.findViewById(R.id.fromSpinner);
toDetails = (Spinner) view.findViewById(R.id.toSpinner);
fromInput = (EditText) view.findViewById(R.id.fromInput);
toInput = (EditText) view.findViewById(R.id.toInput);
日志猫:
Process: com.cloudicalabs.converters, PID: 8063
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/androidRuntime: java.lang.NumberFormatException: Invalid double: ""
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: at java.lang.StringToReal.invalidReal(StringToReal.java:63)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: at java.lang.StringToReal.parseDouble(StringToReal.java:267)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: at java.lang.Double.parseDouble(Double.java:301)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: at java.lang.Double.valueOf(Double.java:338)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: at com.cloudicalabs.converters.fragments.UniqueConverter.onCreateView(UniqueConverter.java:74)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
【问题讨论】:
能否请您发布您的错误行 74 @sun inp = Double.valueOf(fromInput.getText().toString());这是我的错误行 你在fromInput.getText().toString();
中得到一个空字符串,它不能转换成双精度数
你为什么使用Double
和double
?
【参考方案1】:
try
inp = Double.valueOf(fromInput.getText().toString());
catch (NumberFormatException e)
inp = 0;
【讨论】:
【参考方案2】:尝试替换此行
inp = Double.valueOf(fromInput.getText().toString());
与
inp = Double.parseDouble(fromInput.getText().toString());
【讨论】:
出现问题,我的 setAdapter 仅采用 todetails 意味着它在调试时需要很长时间。 我的微调器采用相同的值 你在哪里初始化了那些微调器? 在onInitVies方法中初始化,请查看代码 您是否被NumberFormatException
释放,并且出现了另一个问题或仍然显示相同的错误??【参考方案3】:
@Override
public void afterTextChanged(Editable s)
if (!TextUtils.isEmpty(fromInput.getText())
String value=null;
try
value=String.valueOf(Double.parseDouble(fromInput.getText().toString()));
toInput.setText(value);
catch (NumberFormatException e)
e.printStackTrace();
toInput.setText("");
else
toInput.setText("");
【讨论】:
【参考方案4】:在此之前
inp = Double.valueOf(fromInput.getText().toString());
检查 fromInput 文本中的值是否有“”(空 - 即什么都没有:说像 .length == 0)然后你不应该解析它;而是通知用户输入一些值。 也将其放入 try catch。
【讨论】:
【参考方案5】:您的问题是您要从中获取值的变量或资源为空,因此它说“”不是双重的。如果您从 fromInput.getText().toString()
获取值,则 fromInput 没有值,因此 null 不能转换为 double。
【讨论】:
他是在 initViews() 函数中做的;他会得到 NPE 的基础知识 :) @Parsania Hardik以上是关于java.lang.NumberFormatException:无效的双精度:“”在 java.lang.StringToReal.invalidReal的主要内容,如果未能解决你的问题,请参考以下文章