您好我从edittext获取一个值并转换在其他文本中显示该值但是获取运行时错误java.lang.NumberFormatException:无效的double:“”。at java.lang.StringToReal.invalidReal。在我的setAdapter中,它只取得了意义,这仅仅意味着细节。我不明白这个错误在哪里
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);
}
logcat的:
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)
答案 0 :(得分:2)
try {
inp = Double.valueOf(fromInput.getText().toString());
} catch (NumberFormatException e) {
inp = 0;
}
答案 1 :(得分:2)
尝试替换此行
inp = Double.valueOf(fromInput.getText().toString());
<强>与强>
inp = Double.parseDouble(fromInput.getText().toString());
答案 2 :(得分:0)
您的问题是您想要获取值的变量或资源是null,因此它说&#34; &#34;不是双倍的。如果从fromInput.getText().toString()
获得价值,那么fromInput没有值,因此无法将null转换为加倍。
答案 3 :(得分:0)
@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 :(得分:0)
在此之前
inp = Double.valueOf(fromInput.getText().toString());
检查fromInput文本中的值是否为&#34;&#34; (空-i.e什么都不拿:比如.length == 0)然后你不应该解析它;而是通知用户输入一些值。 也把它放在try catch 。