Android:将 TextView 内容转换为整数然后使用 setText() 时出错

发布于 2024-12-04 02:45:14 字数 5845 浏览 1 评论 0原文

我有一些代码可以获取两个时间选择器的内容并在 TextView 中单独显示它们,但是当我去计算两者之间的差异并将其显示在单独的 TextView 中时,我的应用程序崩溃了。 这是一些代码:

    private TextView mShiftLength;

private TextView mTimeDisplay;
private Button mPickTime;

private TextView mTimeDisplayEnd;
private Button mPickTimeEnd;

    private void calcShiftLength() {
    int mStartTime = Integer.parseInt(mTimeDisplay.getText().toString());
    int mEndTime = Integer.parseInt(mTimeDisplayEnd.getText().toString());

    int shiftLength = mEndTime - mStartTime;

    mShiftLength.setText(Integer.toString(shiftLength));
}

    private void updateDisplay() {
    mTimeDisplay.setText(new StringBuilder().append(pad(mHour)).append(":")
            .append(pad(mMinute)));
}

private void updateDisplayEnd() {
    mTimeDisplayEnd.setText(new StringBuilder().append(pad(mHour))
            .append(":").append(pad(mMinute)));
}

编辑:错误的 logcat。抱歉,我对 Android 不太了解,希望这就是您想要的。

09-13 03:01:39.744: INFO/ActivityManager(59): Starting activity:
 Intent { act=android.intent.action.MAIN
 cat=[android.intent.category.LAUNCHER] flg=0x10000000
 cmp=MultipleTimePickers.UI/.MultipleTimePickers } 09-13 03:01:39.803:
 DEBUG/AndroidRuntime(331): Shutting down VM 09-13 03:01:39.813:
 DEBUG/dalvikvm(331): Debugger has detached; object registry had 1
 entries 09-13 03:01:40.044: INFO/AndroidRuntime(331): NOTE: attach of
 thread 'Binder Thread #3' failed 09-13 03:01:40.174:
 INFO/ActivityManager(59): Start proc MultipleTimePickers.UI for
 activity MultipleTimePickers.UI/.MultipleTimePickers: pid=338
 uid=10050 gids={} 09-13 03:01:41.364: DEBUG/AndroidRuntime(338):
 Shutting down VM 09-13 03:01:41.364: WARN/dalvikvm(338): threadid=1:
 thread exiting with uncaught exception (group=0x4001d800) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338): FATAL EXCEPTION: main 09-13
 03:01:41.393: ERROR/AndroidRuntime(338): java.lang.RuntimeException:
 Unable to start activity
 ComponentInfo{MultipleTimePickers.UI/MultipleTimePickers.UI.MultipleTimePickers}:
 java.lang.NumberFormatException: unable to parse '03:01' as integer
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.access$2300(ActivityThread.java:125) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.os.Handler.dispatchMessage(Handler.java:99) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.os.Looper.loop(Looper.java:123) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.main(ActivityThread.java:4627) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 java.lang.reflect.Method.invokeNative(Native Method) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 java.lang.reflect.Method.invoke(Method.java:521) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 dalvik.system.NativeStart.main(Native Method) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338): Caused by: java.lang.NumberFormatException:
 unable to parse '03:01' as integer 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 java.lang.Integer.parse(Integer.java:433) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 java.lang.Integer.parseInt(Integer.java:422) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 java.lang.Integer.parseInt(Integer.java:382) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 MultipleTimePickers.UI.MultipleTimePickers.calcShiftLength(MultipleTimePickers.java:70)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 MultipleTimePickers.UI.MultipleTimePickers.onCreate(MultipleTimePickers.java:66)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     ... 11 more 09-13
 03:01:41.424: WARN/ActivityManager(59):   Force finishing activity
 MultipleTimePickers.UI/.MultipleTimePickers 09-13 03:01:42.035:
 WARN/ActivityManager(59): Activity pause timeout for
 HistoryRecord{44e7de98 MultipleTimePickers.UI/.MultipleTimePickers}
 09-13 03:01:48.883: DEBUG/dalvikvm(182): GC_EXPLICIT freed 100 objects
 / 4296 bytes in 163ms 09-13 03:01:53.740: WARN/ActivityManager(59):
 Activity destroy timeout for HistoryRecord{44e7de98
 MultipleTimePickers.UI/.MultipleTimePickers} 09-13 03:01:54.034:
 DEBUG/dalvikvm(269): GC_EXPLICIT freed 249 objects / 11840 bytes in
 253ms

最终的工作代码如下,感谢大家的帮助:

    private void calcShiftLength() {
    String[] mStartTime = mTimeDisplay.getText().toString().split(":");
    String[] mEndTime = mTimeDisplayEnd.getText().toString().split(":");

    int mStartHour = Integer.parseInt(mStartTime[0].toString());
    int mStartMinute = Integer.parseInt(mStartTime[1].toString());

    int mEndHour = Integer.parseInt(mEndTime[0].toString());
    int mEndMinute = Integer.parseInt(mEndTime[1].toString());

    int mShiftLengthHour = mEndHour - mStartHour;
    int mShiftLengthMinute = mEndMinute - mStartMinute;

    mShiftLength.setText(new StringBuilder().append(pad(mShiftLengthHour)).append(":").append(pad(mShiftLengthMinute)));
}

I have some code that gets two Time Picker's content and displays them individually in TextViews, but when I go to calculate the difference between the two and display it in a separate TextView my app crashes.
Here's some of the code:

    private TextView mShiftLength;

private TextView mTimeDisplay;
private Button mPickTime;

private TextView mTimeDisplayEnd;
private Button mPickTimeEnd;

    private void calcShiftLength() {
    int mStartTime = Integer.parseInt(mTimeDisplay.getText().toString());
    int mEndTime = Integer.parseInt(mTimeDisplayEnd.getText().toString());

    int shiftLength = mEndTime - mStartTime;

    mShiftLength.setText(Integer.toString(shiftLength));
}

    private void updateDisplay() {
    mTimeDisplay.setText(new StringBuilder().append(pad(mHour)).append(":")
            .append(pad(mMinute)));
}

private void updateDisplayEnd() {
    mTimeDisplayEnd.setText(new StringBuilder().append(pad(mHour))
            .append(":").append(pad(mMinute)));
}

Edit: logcat of error. Sorry I don't know much about Android really, I hope this is all you were after.

09-13 03:01:39.744: INFO/ActivityManager(59): Starting activity:
 Intent { act=android.intent.action.MAIN
 cat=[android.intent.category.LAUNCHER] flg=0x10000000
 cmp=MultipleTimePickers.UI/.MultipleTimePickers } 09-13 03:01:39.803:
 DEBUG/AndroidRuntime(331): Shutting down VM 09-13 03:01:39.813:
 DEBUG/dalvikvm(331): Debugger has detached; object registry had 1
 entries 09-13 03:01:40.044: INFO/AndroidRuntime(331): NOTE: attach of
 thread 'Binder Thread #3' failed 09-13 03:01:40.174:
 INFO/ActivityManager(59): Start proc MultipleTimePickers.UI for
 activity MultipleTimePickers.UI/.MultipleTimePickers: pid=338
 uid=10050 gids={} 09-13 03:01:41.364: DEBUG/AndroidRuntime(338):
 Shutting down VM 09-13 03:01:41.364: WARN/dalvikvm(338): threadid=1:
 thread exiting with uncaught exception (group=0x4001d800) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338): FATAL EXCEPTION: main 09-13
 03:01:41.393: ERROR/AndroidRuntime(338): java.lang.RuntimeException:
 Unable to start activity
 ComponentInfo{MultipleTimePickers.UI/MultipleTimePickers.UI.MultipleTimePickers}:
 java.lang.NumberFormatException: unable to parse '03:01' as integer
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.access$2300(ActivityThread.java:125) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.os.Handler.dispatchMessage(Handler.java:99) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.os.Looper.loop(Looper.java:123) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.main(ActivityThread.java:4627) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 java.lang.reflect.Method.invokeNative(Native Method) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 java.lang.reflect.Method.invoke(Method.java:521) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-13
 03:01:41.393: ERROR/AndroidRuntime(338):     at
 dalvik.system.NativeStart.main(Native Method) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338): Caused by: java.lang.NumberFormatException:
 unable to parse '03:01' as integer 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 java.lang.Integer.parse(Integer.java:433) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 java.lang.Integer.parseInt(Integer.java:422) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 java.lang.Integer.parseInt(Integer.java:382) 09-13 03:01:41.393:
 ERROR/AndroidRuntime(338):     at
 MultipleTimePickers.UI.MultipleTimePickers.calcShiftLength(MultipleTimePickers.java:70)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 MultipleTimePickers.UI.MultipleTimePickers.onCreate(MultipleTimePickers.java:66)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 09-13 03:01:41.393: ERROR/AndroidRuntime(338):     ... 11 more 09-13
 03:01:41.424: WARN/ActivityManager(59):   Force finishing activity
 MultipleTimePickers.UI/.MultipleTimePickers 09-13 03:01:42.035:
 WARN/ActivityManager(59): Activity pause timeout for
 HistoryRecord{44e7de98 MultipleTimePickers.UI/.MultipleTimePickers}
 09-13 03:01:48.883: DEBUG/dalvikvm(182): GC_EXPLICIT freed 100 objects
 / 4296 bytes in 163ms 09-13 03:01:53.740: WARN/ActivityManager(59):
 Activity destroy timeout for HistoryRecord{44e7de98
 MultipleTimePickers.UI/.MultipleTimePickers} 09-13 03:01:54.034:
 DEBUG/dalvikvm(269): GC_EXPLICIT freed 249 objects / 11840 bytes in
 253ms

Here's the final working code, thanks for everyone's help:

    private void calcShiftLength() {
    String[] mStartTime = mTimeDisplay.getText().toString().split(":");
    String[] mEndTime = mTimeDisplayEnd.getText().toString().split(":");

    int mStartHour = Integer.parseInt(mStartTime[0].toString());
    int mStartMinute = Integer.parseInt(mStartTime[1].toString());

    int mEndHour = Integer.parseInt(mEndTime[0].toString());
    int mEndMinute = Integer.parseInt(mEndTime[1].toString());

    int mShiftLengthHour = mEndHour - mStartHour;
    int mShiftLengthMinute = mEndMinute - mStartMinute;

    mShiftLength.setText(new StringBuilder().append(pad(mShiftLengthHour)).append(":").append(pad(mShiftLengthMinute)));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2024-12-11 02:45:14
NumberFormatException: unable to parse '03:01' as integer

尝试使用字符串的 split 方法,然后将每个字符串解析为 Integer。 时,最好捕获 NumberFormatException

在执行 Integer.parseInt http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split%28java.lang.String%29

希望有帮助。

NumberFormatException: unable to parse '03:01' as integer

Try using split method of the string, and then parse each string as Integer. And its alway best to catch the NumberFormatException when doing Integer.parseInt

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split%28java.lang.String%29

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文