0

i have this code to create a CountdownTimer:

 CountDownTimer CountdownTimer = new CountDownTimer(30000, 1000) {
 public void onTick(long millisUntilFinished) {
     txttime.setText(millisUntilFinished / 1000 );
 }

 public void onFinish(){
     timeout_stage();
 }

}.start();

it works fine and i can cancel and start it perfectly, but i want to restart it from another activity. in main form i cancell it, the an activity opens like a dialogbox that has a "AGAIN" button. when i click on that, i want to CountDownTimer.start(); called and my counter restarts again...

how i can do this?

i do this in second activity:

MainActivity main = new MainActivity();
main.CountdownTimer.start();

but i get this error:

04-28 11:42:40.495: E/AndroidRuntime(17039): FATAL EXCEPTION: main
04-28 11:42:40.495: E/AndroidRuntime(17039): java.lang.NullPointerException
04-28 11:42:40.495: E/AndroidRuntime(17039):    at com.english.game.MainActivity$1.onTick(MainActivity.java:70)
04-28 11:42:40.495: E/AndroidRuntime(17039):    at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
04-28 11:42:40.495: E/AndroidRuntime(17039):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 11:42:40.495: E/AndroidRuntime(17039):    at android.os.Looper.loop(Looper.java:137)
04-28 11:42:40.495: E/AndroidRuntime(17039):    at android.app.ActivityThread.main(ActivityThread.java:5041)
04-28 11:42:40.495: E/AndroidRuntime(17039):    at java.lang.reflect.Method.invokeNative(Native Method)
04-28 11:42:40.495: E/AndroidRuntime(17039):    at java.lang.reflect.Method.invoke(Method.java:511)
04-28 11:42:40.495: E/AndroidRuntime(17039):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-28 11:42:40.495: E/AndroidRuntime(17039):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-28 11:42:40.495: E/AndroidRuntime(17039):    at dalvik.system.NativeStart.main(Native Method)
Fcoder
  • 9,066
  • 17
  • 63
  • 100

2 Answers2

0
  1. You can create static handler in Activity1 and start your countDownTimer from there.
  2. In second Activity you can access the static handler instance by Activity1.hander and you can send message from here to start or cancel the countDownTimer.

Don't create the instance of Activity1 by using new operator, instead this use Handler.

Activity1.java

public class Activity1 extends Activity {
public static Handler mHandler; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initHandler();
    mHandler.sendEmptyMessage(1);

    startActivity(new Intent(Activity1.this, Activity2.class));
}

private void initHandler(){
    mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.arg1) {
            case 1:
                mCountDownTimer.start();
                break;
            }
        }
    };
}

private CountDownTimer mCountDownTimer = new CountDownTimer(10000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
        Toast.makeText(Activity1.this, "Count is: "+ millisUntilFinished/1000, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

    }
};
}

Activity2.java

public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Activity1.mHandler.sendEmptyMessage(1);

}
}

This sample code is only idea to do, please use this logic and let me know if any problem is coming...

Vishesh Chandra
  • 6,951
  • 6
  • 35
  • 38
  • i do this but even inside the activity1 mHandler.sendEmptyMessage(1); doesnt work and handleMessage doesnt fired! please help me dear friend, im confused for 2 days – Fcoder Apr 29 '13 at 06:49
0

I am not sure, because there is not enough code to do a proper analysis, but to me these lines look wrong:

MainActivity main = new MainActivity();
main.CountdownTimer.start();

When you initialize the main activity this way, no activity life cycle methods (onCreate(), onStart(), etc.) are called. My guess is, that the txttime (label?) is initialized in onCreate().

Additionally (and imho even worse), you would not restart the timer used in the actual main activity.

To ensure you have the correct main activity in your second activity I woud suggest you use the code from this answer.

Community
  • 1
  • 1
Sascha Kolberg
  • 7,092
  • 1
  • 31
  • 37