尝试从EditText获取double值并在将其传递给另一个Intent之前对其进行操作。不使用原始数据类型,以便可以使用toString方法。
问题是当我包含protein = Double.valueOf(p).doubleValue();样式命令时,程序立即关闭,logcat中没有任何信息。如果我注释掉它们并设置一些虚拟数据,如protein = 1.0;,它可以正常工作。 使用原始数据类型和解析double也会发生同样的情况。这段代码在普通java中使用虚拟数据可以正常工作。 我做错了什么?
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);
kotlin
,您可以像这样使用toDouble()
:val d = textString.toDouble()
。 - Zohab Ali