我在EditText
中输入一个数值。假设我首先输入输入“输入”。然后做toString()
输入。那么parseDouble()
那个。假设结果是输入* 2。所以我在EditText
中设置它。假设,
package com.hasibshovon.unitconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
// data source
private static String[] types = { "Angle", "Area", "Bits&Bytes", "Density",
"Electric Current", "Energy", "Force", "Length", "Mass", "Power",
"Pressure", "Speed", "Temperature", "Time", "Volume" };
private static String[] AngleUnits = { "Degree", "Radian", "Grad", "Gon",
"Minute", "Second", "SIgn", "Mil", "Revolution", "Circle", "Turn",
"Quadrant", "Right", "Triangle", "Sextant" };
private static String[] AreaUnits = { "SquareMeter", "SquareKilometer",
"SquareHectometer", "SquareDekameter", "SquareDecimeter",
"SquareCentimeter", "SquareMilimeter", "SquareMicrometer",
"SquareNanometer", "Hectare", "SquareMile", "SquareYard",
"SquareFoot", "SquareInch", "Acre" };
private static String[] BitsBytesUnits = { "bit", "byte", "kilobit",
"kilobyte", "megabit", "megabyte", "gigabit", "gigabyte",
"terabit", "terabyte", "petabit", "petabyte", "exabit", "exabyte" };
private static String[] DensityUnits = { "kilogram/cubicmeter",
"kilogram/cubiccentimeter", "gram/cubicmeter",
"gram/cubiccentimeter", "gram/cubicmilimeter",
"miligram/cubicmeter", "miligram/cubiccentimeter",
"miligram/cubicmilimeter", "exagram/liter", "petagram/liter",
"teragram/liter", "gigagram/liter", "meggram/liter",
"kilogram/liter", "hectogram/liter", "decagram/liter",
"gram/liter", "decigram/liter", "centigram/liter",
"miligram/liter", "microgram/liter", "nanogram/liter",
"picogram/liter" };
private static String[] CurrentUnits = { "ampere", "kiloampere",
"miliampere", "biot", "abaampere", "stataampere" };
private static String[] EnergyUnits = { "joule", "gigajoule", "megajoule",
"kilojoule", "milijoule", "microjoule", "nanojoule", "attojoule" };
private static String[] ForceUnits = { "newton", "dyne", "joul/meter",
"joul/centimeter", "gram-force", "kilogram-force", "kip-force",
"pond", "kilo-pond" };
private static String[] LengthUnits = { "meter", "exameter", "terameter",
"petameter", "gigameter", "megameter", "kilometer", "hectometer",
"dekameter", "decimeter", "centimeter", "milimeter", "micrometer",
"nanometer", "picometer", "femtometer", "attometer" };
private static String[] MassUnits = { "gram", "kilogram", "miligram",
"dalton", "pound", "ounce", "ton", "quarter", "grain" };
private static String[] PowerUnits = { "watt", "megawatt", "horsepower",
"kilocalorie", "joul/hour", "kilojoul/hour", "joul/second" };
private static String[] PressureUnits = { "pascal", "newton/squaremeter",
"bar", "kilogram-force/squaremeter", "ton-force/squarefoot",
"poundal/squarefoot", "atmosphere(atm)" };
private static String[] SpeedUnits = { "meter/second", "kilometer/hour",
"foot/hour", "yard/hour", "mile/hour", "mile/second", "knot" };
private static String[] TemperatureUnits = { "kelvin", "celsius",
"farenheit", "rankine", "reaumur" };
private static String[] TimeUnits = { "second", "milisecond",
"microsecond", "nanosecond", "minute", "hour", "day", "week",
"month", "year", "decade", "century", "millenium" };
private static String[] VolumeUnits = { "cubicmeter", "cubickilometer",
"cubiccentimeter", "liter", "cc", "barrel", "cup", "cubicmile",
"cubicfoot", "cubicyard", "cubicinch", "acre-foot" };
private Spinner spnUnitTypes;
private Spinner spnFromUnit;
private Spinner spnToUnit;
private EditText etFrom;
private EditText etTo;
public String value1, value2;
public double fromDouble;
public double result;
// adapters
ArrayAdapter<String> adaptUnittype;
ArrayAdapter<String> adaptFrom;
ArrayAdapter<String> adaptTo;
// private OnItemSelectedListener UnitLisetener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// spnUnitTypes.setOnItemSelectedListener(UnitLisetener);
etFrom = (EditText) findViewById(R.id.etConvertFrom);
etTo = (EditText) findViewById(R.id.etToResult);
// initialize view
spnUnitTypes = (Spinner) findViewById(R.id.spnUnitType);
spnFromUnit = (Spinner) findViewById(R.id.spnFromUnit);
spnToUnit = (Spinner) findViewById(R.id.spnToUnit);
// initialize adapter
adaptUnittype = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, types);
// bind adapter and view
spnUnitTypes.setAdapter(adaptUnittype);
spnUnitTypes.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int postion, long arg3) {
if (types[postion].equals("Time")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, TimeUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, TimeUnits);
} else if (types[postion].equals("Mass")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, MassUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, MassUnits);
} else if (types[postion].equals("Angle")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AngleUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AngleUnits);
} else if (types[postion].equals("Area")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AreaUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AreaUnits);
} else if (types[postion].equals("Density")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, DensityUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, DensityUnits);
} else if (types[postion].equals("Bits&Bytes")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
BitsBytesUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
BitsBytesUnits);
} else if (types[postion].equals("Electric Current")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, CurrentUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, CurrentUnits);
} else if (types[postion].equals("Energy")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, EnergyUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, EnergyUnits);
} else if (types[postion].equals("Force")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, ForceUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, ForceUnits);
} else if (types[postion].equals("Length")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, LengthUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, LengthUnits);
} else if (types[postion].equals("Power")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PowerUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PowerUnits);
} else if (types[postion].equals("Pressure")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PressureUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PressureUnits);
} else if (types[postion].equals("Speed")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, SpeedUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, SpeedUnits);
} else if (types[postion].equals("Temperature")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
TemperatureUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
TemperatureUnits);
}
spnFromUnit.setAdapter(adaptFrom);
spnToUnit.setAdapter(adaptTo);
spnFromUnit
.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View arg1, int pos, long arg3) {
Object item = parent.getItemAtPosition(pos);
value1 = item.toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spnToUnit
.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View arg1, int pos, long arg3) {
Object item1 = parent.getItemAtPosition(pos);
value2 = item1.toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
String fromstr = etFrom.getText().toString();
double fromDouble = Double.parseDouble(fromstr);
}
public void convert(View v) {
if (value1.equals("Degree") && value2.equals("Radian")) {
etTo.setText(String.valueOf(fromDouble*2));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:1)
Double.parseDouble()
投放null
或NaN
时会抛出此异常。如果etFrom
填充了除数字之外的任何内容,则会发生这种情况,包括它是空的。
在这种情况下,你可以用fromDouble = Double.parseDouble(fromstr)
块来try-catch
来处理状态。
答案 1 :(得分:1)
您的输入显示为空或不包含要解析的数字。通过使用try / catch
记录或打印出值来尝试调试String fromstr = etFrom.getText().toString();
try {
double fromDouble = Double.parseDouble(fromstr);
} catch (NumberFormatException e) {
System.out.println(fromstr)
}