Frage

I would like to add a seekbar for the time countdown, and so I have coded as follows:

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

But it gives out the following NPE error as shown in the 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

Question:

How could I properly add a seekbar for the CountDownTimer? The NPE error still exists no matter I declare time_progress_bar in Line 311 or not.

Thanks!

War es hilfreich?

Lösung

Try this.

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

    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top