Reputation: 439
I'm trying to use a timer that calls a Class "AysncTask" in a fixed schedule . This timer/class doesn't have anything to do with the GUI or it's elements . When calling the class without timer it works , but with timer I get those errors :
01-04 08:39:39.074: E/AndroidRuntime(394): FATAL EXCEPTION: main
01-04 08:39:39.074: E/AndroidRuntime(394): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projecta/com.example.projecta.MainActivity}: java.lang.RuntimeException: Only one Looper may be created per thread
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.os.Looper.loop(Looper.java:123)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-04 08:39:39.074: E/AndroidRuntime(394): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 08:39:39.074: E/AndroidRuntime(394): at java.lang.reflect.Method.invoke(Method.java:521)
01-04 08:39:39.074: E/AndroidRuntime(394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-04 08:39:39.074: E/AndroidRuntime(394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-04 08:39:39.074: E/AndroidRuntime(394): at dalvik.system.NativeStart.main(Native Method)
01-04 08:39:39.074: E/AndroidRuntime(394): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
01-04 08:39:39.074: E/AndroidRuntime(394): at android.os.Looper.prepare(Looper.java:73)
01-04 08:39:39.074: E/AndroidRuntime(394): at com.example.projecta.MainActivity.onCreate(MainActivity.java:43)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-04 08:39:39.074: E/AndroidRuntime(394): ... 11 more
This is my code so far :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Timer timer = new Timer();
timer.schedule(new UpdateTimeTask(),1, 3000);
class UpdateTimeTask extends TimerTask {
public void run()
{
// do stufff
new status().execute();
}
}
So any idea how to solve this issue ? and thanks in advance
Upvotes: 0
Views: 643
Reputation: 439
Finally found a solution :
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new status().execute();
// PerformBackgroundTask this class is the class that extends AsynchTask
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 10000); //execute in every 50000 ms
}
Upvotes: 1