我想我对这里的活动生命周期感到困惑,我试图执行一个简单的权重转换,没有什么特别的。Android计算传递给onClick方法的值
用户输入一个值,选择他们是否希望它转换为lbs或kgs,当他们按'转换'它应该吐出转换后的值,查找堆栈跟踪它似乎有问题与parseInt im使用。
public class WeightConverter extends Activity implements OnClickListener, OnCheckedChangeListener {
Button convertWeight;
TextView conversionResults;
EditText enterWeight;
RadioGroup weightPicker;
RadioButton radKG, radLB;
double weightValue, convertedWeight;
String weightString;
String measurement;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.weightconverter);
initializeVariables();
}
private void initializeVariables() {
convertWeight = (Button) findViewById(R.id.btnConvertWeight);
conversionResults =(TextView) findViewById(R.id.tvWeightConversion);
enterWeight =(EditText) findViewById(R.id.etWeightToConvert);
weightPicker = (RadioGroup) findViewById(R.id.rgWeightType);
radKG = (RadioButton) findViewById(R.id.radKG);
radLB = (RadioButton) findViewById(R.id.radLB);
convertWeight.setOnClickListener(this);
}
@Override
public void onClick(View v) {
;
switch (v.getId()) {
case R.id.btnConvertWeight:
weightValue = Double.parseDouble(enterWeight.getText().toString());
conversionResults.setText(weightValue + " = " + weightString + measurement);
break;
}
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radKG:
convertedWeight = weightValue/2.2;
measurement = " Kilograms";
break;
case R.id.radLB:
convertedWeight = weightValue * 2.2;
measurement = " Pounds";
break;
}weightString = String.valueOf(convertedWeight);
}
}
我们展示的堆栈跟踪。 – Ahmad 2013-05-08 15:01:12
日食logcat显示错误打开堆栈跟踪没有这样的文件?我如何查看它,这是否与logcat txt文件相同?我的道歉,堆栈跟踪之前显示一个错误,当我coudlnt得到的活动加载,我有'weightValue = Integer.parseInt(enterWeight.getText()。toString());''包含在initializeVariables()中,我将它移动并加载活动 – dyatesupnorth 2013-05-08 15:06:01
如果字符串不是有效数字,解析可能会失败,因此最好将它包装在try-catch块中并捕获任何NumberFormatException。要进行调试,请将字符串写入日志或将其吐出到屏幕,以便您可以看到进入该功能的确切数据。 – NigelK 2013-05-08 15:21:21