Converting String to Double in Android(在Android中将字符串转换为双精度)
问题描述
尝试从 EditText 获取双精度值并在将它们传递给另一个 Intent 之前对其进行操作.不使用原始数据类型,所以我可以使用 toString 方法.
Trying to get double values from an EditText and manipulate them before passing them to another Intent. Not using primitive data type so I can use toString methods.
问题是当我包含 protein=Double.valueOf(p).doubleValue();样式命令,程序强制立即关闭,而不会在 logcat 中留下任何信息.如果我将它们注释掉并设置一些虚拟数据,例如 protein = 1.0;它没有问题.原始数据类型和解析双精度也会发生同样的情况.此代码与普通 java 中的虚拟数据完美配合.我做错了什么?
Problem is when I include the protein=Double.valueOf(p).doubleValue(); style commands, the program force closes immediately without leaving any info in the logcat.If I comment them out and set some dummy data like protein = 1.0; it works with no problems. Same happens with primitive data types and parse double. This code works perfectly with dummy data in normal java. What am I doing wrong?
EditText txtProt, txtCarb, txtFat, txtFiber, txtPoints;
String p, c, f, fi;
Double protein, carbs, fat, fiber;
double temp;
Integer points;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Create Prompt", "ready for layout");
setContentView(R.layout.main);
Log.v("Layout Created", "ready for variable assignment");
txtProt = (EditText) findViewById(R.id.Protein);
txtCarb = (EditText) findViewById(R.id.Carbs);
txtFat = (EditText) findViewById(R.id.Fat);
txtFiber = (EditText) findViewById(R.id.Fiber);
txtPoints = (EditText) findViewById(R.id.Points);
btnCalc = (Button) findViewById(R.id.Calc);
Log.v("Variables Assigned", "ready for double assignment");
p = txtProt.getText().toString();
c = txtCarb.getText().toString();
f = txtFat.getText().toString();
fi = txtFiber.getText().toString();
protein=Double.valueOf(p).doubleValue();
carbs=Double.valueOf(c).doubleValue();
fat=Double.valueOf(f).doubleValue();
fiber=Double.valueOf(fi).doubleValue();
Log.v("Doubles parsed", "ready for calculations");
//these are the problem statements
protein = 1.0;
carbs = 1.0;
fat = 1.0;
fiber = 1.0;
protein *= 16;
carbs *= 19;
fat *= 45;
fiber *= 14;
temp = protein + carbs + fat - fiber;
temp = temp/175;
points = new Integer((int) temp);
推荐答案
我会这样做:
try {
txtProt = (EditText) findViewById(R.id.Protein); // Same
p = txtProt.getText().toString(); // Same
protein = Double.parseDouble(p); // Make use of autoboxing. It's also easier to read.
} catch (NumberFormatException e) {
// p did not contain a valid double
}
程序强制立即关闭,而不会在 logcat 中留下任何信息"
"the program force closes immediately without leaving any info in the logcat"
我不知道是否不会在 logcat 输出中留下信息,但强制关闭通常意味着存在未捕获的异常 - 例如 NumberFormatException.
I don't know bout not leaving information in the logcat output, but a force-close generally means there's an uncaught exception - like a NumberFormatException.
这篇关于在Android中将字符串转换为双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在Android中将字符串转换为双精度
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01