I'm using a Runnable
as a Timer
that alternatively runs a block.
I got this code from here and changed it as such:
public class TikingTimer {
private long countDownInterval;
private boolean status;
public TikingTimer(long pCountDownInterval) {
this.countDownInterval = pCountDownInterval;
status = false;
Initialize();
}
public void Stop() {
status = false;
}
public void Start() {
status = true;
}
public void Initialize()
{
final Handler handler = new Handler();
final Runnable counter = new Runnable(){
public void run(){
if(status) {
//TODO my code
} else {
handler.postDelayed(this, countDownInterval);
}
}
};
handler.postDelayed(counter, countDownInterval);
}
}
... and at onCreate
method I used :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TikingTimer tt = new TikingTimer(1000);
tt.Start();
}
But when I run the project, it force-closes.
Is it wrong to use Runnable
at onCreate
method?
Thanks.
The LogCat stack trace is:
08-11 13:04:55.384: E/AndroidRuntime(3125): FATAL EXCEPTION: main
08-11 13:04:55.384: E/AndroidRuntime(3125): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{kawthar.taghvimdigital/kawthar.taghvimdigital.MainActivity}: java.lang.NullPointerException
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.os.Looper.loop(Looper.java:137)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-11 13:04:55.384: E/AndroidRuntime(3125): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 13:04:55.384: E/AndroidRuntime(3125): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 13:04:55.384: E/AndroidRuntime(3125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-11 13:04:55.384: E/AndroidRuntime(3125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-11 13:04:55.384: E/AndroidRuntime(3125): at dalvik.system.NativeStart.main(Native Method)
08-11 13:04:55.384: E/AndroidRuntime(3125): Caused by: java.lang.NullPointerException
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.Activity.findViewById(Activity.java:1839)
08-11 13:04:55.384: E/AndroidRuntime(3125): at kawthar.taghvimdigital.MainActivity.<init>(MainActivity.java:14)
08-11 13:04:55.384: E/AndroidRuntime(3125): at java.lang.Class.newInstanceImpl(Native Method)
08-11 13:04:55.384: E/AndroidRuntime(3125): at java.lang.Class.newInstance(Class.java:1319)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-11 13:04:55.384: E/AndroidRuntime(3125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-11 13:04:55.384: E/AndroidRuntime(3125): ... 11 more