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

时间:2011-09-12 16:57:13

标签: android string integer

我有一些代码可以获得两个Time Picker的内容,并在TextViews中单独显示它们,但是当我计算两者之间的差异并将其显示在单独的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)));
}

1 个答案:

答案 0 :(得分:0)

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

尝试使用字符串的split方法,然后将每个字符串解析为Integer。并且它最好在执行Integer.parseInt

时捕获NumberFormatException

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

希望有所帮助。