大家好,我有下面的代码应该倒计时到某个日期,但我在ontick的某个地方得到NullPointerException
。有谁可以指出问题出在哪里?以下是将在 MainActivity.java 中显示的倒数计时器。
GregorianCalendar gc = new GregorianCalendar();
long currentTime = gc.getTimeInMillis();
GregorianCalendar futureDate = new GregorianCalendar();
futureDate.set(2015, 10, 14, 0, 0, 0);
long difference = futureDate.getTimeInMillis() - currentTime;
long x = difference/1000;
final long second = x % 60;
x = x/60;
long minutes = x%60;
x /= 60;
long hour = x % 24;
x /= 24;
long days = x;
//end of timer logic to use
// injecting into the user interface
timerDays = (TextView) findViewById(R.id.timerday);
myClock = new CountDownTimer(difference, 1000) {
@Override
public void onTick(long millisUntilFinished) {
String secs = Long.toString(second);
timerDays.setText(secs);
}
@Override
public void onFinish() {
timerDays.setText("done!");
}
}.start();
Logcat消息如下:
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: FATAL EXCEPTION: main
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: Process: com.iantheninja.ndw, PID: 16925
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iantheninja.ndw/com.iantheninja.ndw.MainActivity}: java.lang.NullPointerException
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:154)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:110)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5328)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: Caused by: java.lang.NullPointerException
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at com.iantheninja.ndw.MainActivity.onCreate(MainActivity.java:55)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5279)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:154)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:110)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5328)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
11-06 15:44:32.915 16925-16925/com.iantheninja.ndw E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
timerDays = (TextView) findViewById(R.id.timerday);
您可能没有View的ID为R.id.timerday
。