How to open AsyncTask from a Thread

Posted by Abhishek on Stack Overflow See other posts from Stack Overflow or by Abhishek
Published on 2012-09-01T21:11:40Z Indexed on 2012/09/01 21:38 UTC
Read the original article Hit count: 300

In my application I have created a SplashScreen that will b shown for 5 Seconds and after that it executes an if else case depending upon the values stored in the Preference file. If Preference file contains values then the AsyncTask code will run else the Login form will load. When i try to run my application. The thread is going to the Login form with the help of intent but when it comes to AsyncTask my application shows a force close error message.

This is my SplashScreen code:

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);

    Thread timer = new Thread()
    {
        public void run()
        {
            try
            {
                sleep(5000);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
            finally
            {
                if(GoGolfPref.getEmail(SplashScreen.this)!=null && GoGolfPref.getPass(SplashScreen.this)!=null)
                {
                    new LoadingScreen(SplashScreen.this, SplashScreen.this).execute("login_page", Login.url+GoGolfPref.getEmail(SplashScreen.this)+"/"+GoGolfPref.getPass(SplashScreen.this));
                }
                else
                {
                    Intent in = new Intent(SplashScreen.this, Login.class);
                    startActivity(in);
                    finish();
                }
            }
        }
    };
    timer.start();
}

}

This is the error I am getting:

08-29 07:25:58.040: E/AndroidRuntime(2365): FATAL EXCEPTION: Thread-10
08-29 07:25:58.040: E/AndroidRuntime(2365): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-29 07:25:58.040: E/AndroidRuntime(2365):     at android.os.Handler.<init>(Handler.java:121)
08-29 07:25:58.040: E/AndroidRuntime(2365):     at android.app.Dialog.<init>(Dialog.java:101)
08-29 07:25:58.040: E/AndroidRuntime(2365):     at android.app.AlertDialog.<init>(AlertDialog.java:63)
08-29 07:25:58.040: E/AndroidRuntime(2365):     at android.app.ProgressDialog.<init>(ProgressDialog.java:80)
08-29 07:25:58.040: E/AndroidRuntime(2365):     at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
08-29 07:25:58.040: E/AndroidRuntime(2365):     at com.pnf.gogolf.LoadingScreen.<init>(LoadingScreen.java:130)
08-29 07:25:58.040: E/AndroidRuntime(2365):     at com.pnf.gogolf.SplashScreen$1.run(SplashScreen.java:32)

How to get this working?

Thanks in advance...

© Stack Overflow or respective owner

Related posts about android

Related posts about multithreading