math.round函数异常

时间:2018-10-12 18:41:40

标签: java exception math rounding

我恰好在这一行出现了一个异常,无法弄清楚这是怎么回事。

SecondsText.setText(Math.round(SecondsTime));

这是其余的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    long AnDate = 1476650909;
    TextView SecondsText = (TextView) findViewById(R.id.SecondsText);
    TextView MinutesText = (TextView) findViewById(R.id.MinutesText);
    TextView HoursText = (TextView) findViewById(R.id.HoursText);
    TextView DaysText = (TextView) findViewById(R.id.DaysText);
    long SecondsTime = System.currentTimeMillis()/1000 - AnDate;
    long MinutesTime = SecondsTime/60;
    long HoursTime = MinutesTime/60;
    long DaysTime = HoursTime/24;
    **SecondsText.setText(Math.round(SecondsTime));**
    MinutesText.setText(Math.round(MinutesTime));
    HoursText.setText(Math.round(HoursTime));
    DaysText.setText(Math.round(DaysTime));
}

例外说明

> 10-12 15:32:25.920 9254-9254/com.example.mauro.howmuchtime E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mauro.howmuchtime, PID: 9254
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mauro.howmuchtime/com.example.mauro.howmuchtime.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x3bd011c
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2792)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:172)
    at android.app.ActivityThread.main(ActivityThread.java:6590)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x3bd011c
    at android.content.res.Resources.getText(Resources.java:339)
    at android.widget.TextView.setText(TextView.java:5496)
    **at com.example.mauro.howmuchtime.MainActivity.onCreate(MainActivity.java:24)**
    at android.app.Activity.performCreate(Activity.java:7023)
    at android.app.Activity.performCreate(Activity.java:7014)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:172) 
    at android.app.ActivityThread.main(ActivityThread.java:6590) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

java.lang.Math被导入,SecondssText是Android Studio中的textview对象,SecondsTime是一个长变量,我希望从中删除小数。我知道这是一个非常愚蠢的问题,但无法真正弄清楚,因为我是Java新手。谢谢!

1 个答案:

答案 0 :(得分:1)

SecondsTimefloat,它将调用round()的重载版本,并返回int。接受整数的setText()方法将参数定义为:

  

要显示的字符串资源的资源标识符。

由于您传递的int不是资源标识符,因此您需要将其转换为String才能调用setText(CharSequence text)版本。您可以使用String.valueOf()方法进行此操作:

SecondsText.setText(String.valueOf(Math.round(SecondsTime)));