小项目中的Android [英] Small project in Android
本文介绍了小项目中的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
双temp_val = 0.0;
temp_val = Double.parseDouble(et1.getText()的toString());
如果(temp_val&下; 14){
最后AlertDialog alertDialog =新AlertDialog.Builder(本).create();
alertDialog.setTitle(复位...);
alertDialog.setMessage(白平衡应大于或等于14);
alertDialog.setButton2(OK,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
//这里你可以添加功能
dialog.dismiss();
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
et1.setText();
tv1.setText(WB);
返回;
}
双P = 1.8;
双F值= P;
temp_val = temp_val * F值;
temp_val =(temp_val + 32);字符串temp_val1 = Double.toString(temp_val);
wbval =的Integer.parseInt(temp_val1);
和我得到了以下异常,请给我的解决方案
07-31 14:27:35.558:ERROR / AndroidRuntime(375):致命异常:主要
07-31 14:27:35.558:ERROR / AndroidRuntime(375):java.lang.NumberFormatException:无法解析''作为整数
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在java.lang.Integer.parseInt(Integer.java:412)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在java.lang.Integer.parseInt(Integer.java:382)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在com.ExtraCharge.Calc.onClick(Calc.java:744)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在android.view.View.performClick(View.java:2408)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在android.view.View $ PerformClick.run(View.java:8816)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在android.os.Handler.handleCallback(Handler.java:587)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在android.os.Handler.dispatchMessage(Handler.java:92)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在android.os.Looper.loop(Looper.java:123)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在android.app.ActivityThread.main(ActivityThread.java:4627)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在java.lang.reflect.Method.invokeNative(本机方法)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在java.lang.reflect.Method.invoke(Method.java:521)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-31 14:27:35.558:ERROR / AndroidRuntime(375):在dalvik.system.NativeStart.main(本机方法)
解决方案
您的文本字段的值转换三次:
temp_val = Double.parseDouble(et1.getText()的toString());
...
字符串temp_val1 = Double.toString(temp_val);
wbval =的Integer.parseInt(temp_val1);
你为什么这样做呢?这不会很好地工作,因为如果temp_val保存双,那么temp_val1将举行双,最后谈话会失败。这是发生了什么,但是:
事实上,temp_val1持有空字符串,这就是为什么异常是在最后一次谈话抛出。
Double temp_val = 0.0;
temp_val = Double.parseDouble(et1.getText().toString());
if (temp_val < 14) {
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Reset...");
alertDialog.setMessage("WB should be greater than or equal to 14");
alertDialog.setButton2("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
dialog.dismiss();
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
et1.setText("");
tv1.setText("WB");
return;
}
double p = 1.8;
double fvalue = p;
temp_val = temp_val * fvalue;
temp_val = (temp_val + 32);
String temp_val1 = Double.toString(temp_val);
wbval = Integer.parseInt(temp_val1);
and I got the following Exception, please give me solution
07-31 14:27:35.558: ERROR/AndroidRuntime(375): FATAL EXCEPTION: main
07-31 14:27:35.558: ERROR/AndroidRuntime(375): java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at java.lang.Integer.parseInt(Integer.java:412)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at java.lang.Integer.parseInt(Integer.java:382)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at com.ExtraCharge.Calc.onClick(Calc.java:744)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at android.view.View.performClick(View.java:2408)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at android.view.View$PerformClick.run(View.java:8816)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at android.os.Handler.handleCallback(Handler.java:587)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at android.os.Looper.loop(Looper.java:123)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at java.lang.reflect.Method.invoke(Method.java:521)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-31 14:27:35.558: ERROR/AndroidRuntime(375): at dalvik.system.NativeStart.main(Native Method)
解决方案
You are converting the value of the text-field three times:
temp_val = Double.parseDouble(et1.getText().toString());
...
String temp_val1=Double.toString(temp_val);
wbval = Integer.parseInt(temp_val1);
why do you do that? That wont work very well because if temp_val holds a double, then temp_val1 will hold a double, and the last conversation will fail. That is what happens, but:
In fact, temp_val1 holds an empty string, that's why the exception is thrown at the last conversation.
这篇关于小项目中的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文