Answer the question
In order to leave comments, you need to log in
How to stop timer when Activity is closed?
In onCreate , a timer is created that starts an AsyncTask thread every 15 seconds, after the activity or the entire application closes, the timer still continues to start this thread. If the timer.cancel(); command is executed in onDestroy or on a button click, the activity throws an error.
here is the timer code
in onCreate
Timer timer = new Timer();
MyTimerTask task = new MyTimerTask();
timer.schedule(task,0, 15 * 1000);
}
class MyTimerTask extends TimerTask {
public void run() {
String streamUrl = "ссылка";
metadataTask =new MetadataTask();
try {
metadataTask.execute(new URL(streamUrl));
} catch (MalformedURLException e) {
e.printStackTrace();
}
String title_artist;
try {
streamMeta.refreshMeta();
title_artist = streamMeta.getStreamTitle();
Log.i("ARTIST TITLE", title_artist);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
12-31 19:09:04.220: E/AndroidRuntime(1816): FATAL EXCEPTION: main
12-31 19:09:04.220: E/AndroidRuntime(1816): java.lang.RuntimeException: Unable to destroy activity {com.example .kgw/com.example.kgw.TimerActivity}: java.lang.NullPointerException
12-31 19:09:04.220: E/AndroidRuntime(1816): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3499)
12- 31 19:09:04.220: E/AndroidRuntime(1816): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3517)
12-31 19:09:04.220: E/AndroidRuntime(1816): at android.app. ActivityThread.access$1200(ActivityThread.java:141)
12-31 19:09:04.220: E/AndroidRuntime(1816): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
12-31 19:09:04.220: E/AndroidRuntime(1816): at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 19:09:04.220: E/AndroidRuntime(1816): at android. os.Looper.loop(Looper.java:137)
12-31 19:09:04.220: E/AndroidRuntime(1816): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-31 19:09: 04.220: E/AndroidRuntime(1816): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 19:09:04.220: E/AndroidRuntime(1816): at java.lang.reflect.Method.invoke( Method.java:525)
12-31 19:09:04.220: E/AndroidRuntime(1816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-31 19:09: 04.220: E/AndroidRuntime(1816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-31 19:09:04.220: E/AndroidRuntime(1816): at dalvik.system.NativeStart.main(Native Method)
12-31 19:09:04.220: E/AndroidRuntime(1816): Caused by: java.lang .NullPointerException
12-31 19:09:04.220: E/AndroidRuntime(1816): at com.example.kgw.TimerActivity.onDestroy(TimerActivity.java:313)
12-31 19:09:04.220: E/AndroidRuntime(1816) : at android.app.Activity.performDestroy(Activity.java:5302)
12-31 19:09:04.220: E/AndroidRuntime(1816): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
12-31 19:09:04.220: E/AndroidRuntime(1816): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3486)
12-31 19:09:04.220: E/AndroidRuntime(1816): ... 11 more
Answer the question
In order to leave comments, you need to log in
You most likely have the variable timer == null at the time of window destruction. Are you sure you set it globally? Because here in onCreate you seem to have a local variable.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question