タイムピッカーのコンテンツを2つ取得して個々にTextViewsで表示するコードがありますが、別のTextView私のアプリケーションがクラッシュします。Android:TextViewコンテンツを整数に変換してからsetText()を使用するときにエラーが発生する
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)));
}
エラーは何ですか?ログキャッチ? –
logcatを追加しました。うまくいけばそれはあなたが望んでいたものです。 – Trav
あなたの答えがあります: '09-13 03:01:41.393:ERROR/AndroidRuntime(338):java.lang.RuntimeException:アクティビティを開始できませんComponentInfo {MultipleTimePickers.UI/MultipleTimePickers.UI.MultipleTimePickers}:java.lang.RuntimeException: NumberFormatException:'03:01 'を整数として解析できません' –