开发者

Android: Error when converting TextView content to integer and then using setText()

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)));
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜