字符串转换到Android的翻番 [英] Converting String to Double in Android
问题描述
试图从一个EditText获得双倍值,并将其传递到另一个意图之前,操作它们。不使用基本数据类型,这样我就可以使用的toString方法。
问题是,当我包括蛋白质= Double.valueOf(P).doubleValue();风格的命令,程序紧接关闭,不留任何信息的logcat.If我把它们注释掉,并设置样蛋白= 1.0一些虚拟的数据;它的工作原理没有问题。 同样的情况与基本数据类型并解析双。这code完美的作品与普通Java虚拟数据。 我究竟做错了什么?
的EditText txtProt,txtCarb,txtFat,txtFiber,txtPoints;
串P,C,F,网络连接;
双蛋白质,碳水化合物,脂肪,纤维;
双温度;
整数关口;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
Log.v(创建提示,准备布局);
的setContentView(R.layout.main);
Log.v(布局创建,准备好变量赋值);
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 =(按钮)findViewById(R.id.Calc);
Log.v(变量分配,准备好双重分配);
P = txtProt.getText()的toString();
C = txtCarb.getText()的toString();
F = txtFat.getText()的toString()。
。网络= txtFiber.getText()的toString();
蛋白质= Double.valueOf(p)的.doubleValue();
碳水化合物= Double.valueOf(C).doubleValue();
脂肪= Double.valueOf(F).doubleValue();
纤维= Double.valueOf(FI).doubleValue();
Log.v(双打解析,准备计算);
//这些都是问题陈述
蛋白质= 1.0;
碳水化合物= 1.0;
脂肪= 1.0;
纤维= 1.0;
蛋白质* = 16;
碳水化合物* = 19;
脂肪* = 45;
光纤* = 14;
TEMP =蛋白质+碳水化合物+脂肪 - 纤维;
TEMP =温度/ 175;
分=新的整数((INT)临时);
我会做这种方式:
尝试{
txtProt =(EditText上)findViewById(R.id.Protein); // 相同
P = txtProt.getText()的toString(); // 相同
蛋白质= Double.parseDouble(对); //使使用自动装箱的。它也更容易阅读。
}赶上(NumberFormatException异常E){
//没有P包含一个有效的双
}
编辑:程序紧接关闭,不留任何信息的logcat的
我不知道回合没有离开信息的logcat的输出,而是一种力量,密切通常意味着有一个未捕获的异常 - 就像一个NumberFormatException异常
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.
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);
I would do it this way:
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
}
EDIT: "the program force closes immediately without leaving any info in the logcat"
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的翻番的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!