我有一个editText(称为输入),用户输入一个值,然后选择一个单选按钮将该值转换为另一个度量单位。我正在尝试设置一个if语句,以便在没有选择单选按钮并且edittext为空但它不断崩溃程序时显示一条短信!
public void startCalculation() {
RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup);
int selectedConversion = rg.getCheckedRadioButtonId();
double input = Double.parseDouble(((EditText) findViewById(R.id.currentSpeed)).getText().toString());
double speed = 0;
String measurement = "";
System.out.println("The Value of Input is: " + input);
//If its 0 then no unit has been selected
if(selectedConversion != -1 && input > 0) {
任何导致程序崩溃的想法?
编辑:这是日志:
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: FATAL EXCEPTION: main
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: Process: com.example.dan14.windproject, PID: 2058
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: java.lang.NumberFormatException: Invalid double: ""
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at java.lang.StringToReal.invalidReal(StringToReal.java:63)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at java.lang.StringToReal.parseDouble(StringToReal.java:267)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at java.lang.Double.parseDouble(Double.java:301)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at com.example.dan14.windproject.MainActivity.startCalculation(MainActivity.java:88)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at com.example.dan14.windproject.MainActivity$2.onClick(MainActivity.java:49)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at android.view.View.performClick(View.java:5198)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21147)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:2)
问题是这行代码:
double input = Double.parseDouble(((EditText) findViewById(R.id.currentSpeed)).getText().toString());
你传递一个空字符串,这会导致像你的情况一样的NumberFormatException。
使用该代码使其更具可读性:
public void startCalculation() {
RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup);
int selectedConversion = rg.getCheckedRadioButtonId();
String lInputString = ((EditText) findViewById(R.id.currentSpeed)).getText().toString();
double input = 0;
if (!lInputString.trim().equals("")){
input = Double.parseDouble(lInputString);
}
double speed = 0;
String measurement = "";
System.out.println("The Value of Input is: " + input);
//If its 0 then no unit has been selected
if(selectedConversion != -1 && input > 0) {
答案 1 :(得分:0)
您的public override IEnumerable<Contact> Fill(IEnumerable<int> ids)
{
IEnumerable<Contact> result = null;
if (ids != null && ids.Count() > 0)
{
using (IDbConnection dbConnection = ConnectionProvider.OpenConnection())
{
dbConnection.Open();
var predicate = Predicates.Field<Contact>(f => f.id, Operator.Eq, ids);
result = dbConnection.GetList<Contact>(predicate);
dbConnection.Close();
}
}
return result;
}
为空,因此尝试解析时出错。检查String是否为空
String
或捕获NumberFormatException
String text = ((EditText) findViewById(R.id.currentSpeed)).getText().toString();
if(!text.isEmpty()) {
double input = Double.parseDouble(text);
}
答案 2 :(得分:0)
public void startCalculation() {
RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup);
int selectedConversion = rg.getCheckedRadioButtonId();
try{
double input = Double.parseDouble(((EditText) findViewById(R.id.currentSpeed)).getText().toString());
}catch(Exception e)
{
//if catch block is execute it means edittext is empty
}
double speed = 0;
String measurement = "";
System.out.println("The Value of Input is: " + input);
//If its 0 then no unit has been selected
if(selectedConversion != -1 && input > 0) {