计时器对弦的价值

时间:2012-07-17 14:21:40

标签: java android string chronometer

我正在尝试将我的天文台表的值乘以或者只是尝试将该值设为字符串。

每次我尝试将其设为字符串并使用toast.show()来查看它时,我的应用程序崩溃了。我可以得到我的EditText的价值,但我似乎无法让我的天文台工作。字符串timefin都会导致崩溃。这是代码:

String hour = String.valueOf(mText.getText());
String time = String.valueOf(mChronometer.getBase() - stoppedMilliseconds);
int h = java.lang.Integer.parseInt(hour) / 60;
int t = java.lang.Integer.parseInt(time) * 1000 / 60;
int mm = h * t;
mon.setText(Integer.toString(mm));
String fin = "" + mm;


Toast.makeText(PoopActivity.this,
        "Money made: " + fin, Toast.LENGTH_LONG).show();

1 个答案:

答案 0 :(得分:0)

有几个缺点模糊了这段代码的功能。

  • time总是消极的。交换减法参数。
  • hour从小时转换为分钟时,乘以而不是除以60。
  • time从毫秒转换为分钟时,除以而不是乘以1000。
  • mm的计算产生以分钟为单位的数量。这几乎没有任何意义。
  • 使用getApplicationContext代替PoopActivityPoopActivity以外的某些应用程序可能正在运行。
  • 没有异常处理。据我们所知,mon可能为空,setText会抛出异常。将代码包装在try - finally块中:

String fin = "";

try
{
    // most of your code goes here

    mon.setText(Integer.toString(mm));
    String fin += mm;
}
catch (Exception e)
{
    // display the exception here if so inclined
}
finally
{
    Toast.makeText(PoopActivity.this,
    "Money made: " + fin, Toast.LENGTH_LONG).show();
}

try { // most of your code goes here mon.setText(Integer.toString(mm)); String fin += mm; } catch (Exception e) { // display the exception here if so inclined } finally { Toast.makeText(PoopActivity.this, "Money made: " + fin, Toast.LENGTH_LONG).show(); }

但是,是一个包含负数的字符串。