Android:CountDownTimer的Seekbar

时间:2014-01-26 15:07:24

标签: android nullpointerexception seekbar countdowntimer

我想为时间倒计时添加一个搜索栏,所以我编码如下:

public class MyCountDownTimer extends CountDownTimer 
{
    public MyCountDownTimer(long startTime, long interval) 
    {
        super(startTime, interval);
        time_progress_bar = (SeekBar) findViewById(R.id.time_progress_bar);
        time_progress_bar.setMax((int) (startTime));  //LINE 312    
    }

    @Override
    public void onFinish()
    {
        buttonNext.setText("GAME OVER!");
        timesup_high_score_dialog();
    }

    @Override
    public void onTick(long millisUntilFinished) 
    {
        time_remaining = millisUntilFinished;
        buttonNext.setText("   " + millisUntilFinished / 1000 + "." + millisUntilFinished % 1000);
        time_progress_bar.setProgress((int) (time_remaining));  
    }
}

但是它给出了以下NPE错误,如Logcat所示:

01-26 23:03:44.271: E/AndroidRuntime(18951): FATAL EXCEPTION: main
01-26 23:03:44.271: E/AndroidRuntime(18951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.abc/com.abc.abc.Game_middle}: java.lang.NullPointerException
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.app.ActivityThread.access$700(ActivityThread.java:168)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.os.Looper.loop(Looper.java:137)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.app.ActivityThread.main(ActivityThread.java:5493)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at java.lang.reflect.Method.invokeNative(Native Method)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at java.lang.reflect.Method.invoke(Method.java:525)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at dalvik.system.NativeStart.main(Native Method)
01-26 23:03:44.271: E/AndroidRuntime(18951): Caused by: java.lang.NullPointerException
01-26 23:03:44.271: E/AndroidRuntime(18951):    at com.abc.abc.Game_middle$MyCountDownTimer.<init>(Game_middle.java:312)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at com.abc.abc.Game_middle.onCreate(Game_middle.java:153)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.app.Activity.performCreate(Activity.java:5372)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-26 23:03:44.271: E/AndroidRuntime(18951):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
01-26 23:03:44.271: E/AndroidRuntime(18951):    ... 11 more

问题:

如何为CountDownTimer正确添加搜索栏? 无论我是否在311行声明了time_progress_bar,NPE错误仍然存​​在。

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个。

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getName();
    private TextView mTxtTime;
    private SeekBar mSkbCountDown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTxtTime = (TextView) findViewById(R.id.txt_time);
        mSkbCountDown = (SeekBar) findViewById(R.id.skb_count_down);
        mSkbCountDown.setProgress(mSkbCountDown.getMax());

        new MyCountDown(100000, 1000).start();
    }

    public class MyCountDown extends CountDownTimer {

        public MyCountDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            mSkbCountDown.setMax((int) millisInFuture);
        }

        @Override
        public void onFinish() {
            mTxtTime.setText("Game Over!!");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            long timeRemaining = millisUntilFinished;
            mTxtTime.setText("   " + millisUntilFinished / 1000 + "."
                    + millisUntilFinished % 1000);
            mSkbCountDown.setProgress((int) (timeRemaining));
            Log.i(TAG, "onTick: " + millisUntilFinished);
        }

    }
}