调用CountDownTimer方法会推送Null指针异常

时间:2014-01-03 02:43:40

标签: android nullpointerexception static-methods countdowntimer

我正在研究一个倒计时器,它在调用方法时似乎吐出一个空指针异常。请参阅下面的logcat输出:

    01-02 21:26:50.772: D/AndroidRuntime(1743): Shutting down VM
    01-02 21:26:50.772: W/dalvikvm(1743): threadid=1: thread exiting with uncaught exception (group=0xb4a72ba8)
    01-02 21:26:50.842: E/AndroidRuntime(1743): FATAL EXCEPTION: main
    01-02 21:26:50.842: E/AndroidRuntime(1743): Process: com.vertygoeclypse.multitimer, PID: 1743
    01-02 21:26:50.842: E/AndroidRuntime(1743): java.lang.NullPointerException
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at com.vertygoeclypse.multitimer.MainActivity.onClick(MainActivity.java:93)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at android.view.View.performClick(View.java:4438)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at android.view.View$PerformClick.run(View.java:18422)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at android.os.Handler.handleCallback(Handler.java:733)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at android.os.Handler.dispatchMessage(Handler.java:95)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at android.os.Looper.loop(Looper.java:136)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at java.lang.reflect.Method.invoke(Method.java:515)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    01-02 21:26:50.842: E/AndroidRuntime(1743):     at dalvik.system.NativeStart.main(Native Method)
    01-02 21:26:53.162: I/Process(1743): Sending signal. PID: 1743 SIG: 9

logcat向我显示空指针异常来自我的代码的第93行,这一行在下面:

    countDownTimer.start();

链接到以下初始化声明:

    MultiCountDownTimer countDownTimer;

引导我们现在这个代码指向一个CountDownTimer,其中onfinish()和onTick()由我自己的行覆盖,请参阅下面的代码:

        public class MultiCountDownTimer extends CountDownTimer
{
    public MultiCountDownTimer(long startTime, long interval)
        {
            super(startTime, interval);
        }
    @Override
    public void onFinish()
        {
            timeRemaining.setText("Time's up!");
            timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
        }
    @Override
    public void onTick(long millisUntilFinished)
        {
            timeRemaining.setText("Time remain:" + millisUntilFinished);
            timeElapsed = startTime - millisUntilFinished;
            timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
        }
}

我从带有numberpickers的对话框中设置的值设置了两个要求。我log.i 2个占位符变量,他们返回所需的长值。请参阅下面的设置所需值的代码:

        long startTime=0;
long interval=1000;
long coversionvalues=0;

这些是初始化者。

            case R.id.submitbtn:
        tgview.setText(String.valueOf(tagvalue.getText()));
        minview.setText(String.valueOf(minnp.getValue()));
        secview.setText(String.valueOf(secnp.getValue()));
        int val1 = Integer.parseInt(String.valueOf(minnp.getValue()));
        int val2 = Integer.parseInt(String.valueOf(secnp.getValue()));
        int val3 = (val1*60)*1000;
        int val4 = val2*1000;
        coversionvalues = Long.valueOf(String.valueOf(val3+val4));
        startTime = coversionvalues;
        starest.setEnabled(true);
        cusd.dismiss(); 
        break;

在开关中使用onclick以获取将值设置为屏幕上的某些占位符的情况,以及将Countpicker值转换为Long以用于CountDownTimer。之后,启用倒计时的nutton启用并关闭对话框。那个案子坏了。这很好,它是启动计时器的部分,引导我到这一步。

非常感谢您提供的所有帮助。

问候

cchinchoy

1 个答案:

答案 0 :(得分:1)

MultiCountDownTimer countDownTimer;

不是初始化声明(它是声明声明)。

MultiCountDownTimer countDownTimer = new MultiCountDownTimer();

是,你在哪里初始化countDown Timer,它是否在onClick代码之前发生?