Lou Morda
Lou Morda

Reputation: 5165

AsyncTask already started onCreate

When I try to execute an AsyncTask from a button click, my app says the task is already started. I don't execute the task anywhere in onCreate or onResume or anything like that. Can a task be started when my activity is created without me executing it programatically?

    // Login button
    Button loginButton = (Button) findViewById(R.id.loginBtn);
    loginButton.setOnClickListener(new Button.OnClickListener() 
    {
        public void onClick(View v) 
        {                
            loginTask.execute("");
        }
    });


// Background login thread
private AsyncTask<Object, String, String> loginTask = new AsyncTask<Object, String, String>(){

    @Override
    protected String doInBackground(Object... arg0) 
    {
        Log.v(TAG, "Login task started");
        LoginManager lm = new LoginManager();
        String username = emailText.getText().toString();
        String password = passwordText.getText().toString();
        loginBundle = new Bundle();
        loginBundle.putString(Constants.LOGIN_HANDLER_ID, lm.login(username, password));
        loginMessage = Message.obtain(null, 0);
        loginMessage.setData(loginBundle);
        loginHandler.sendMessage(loginMessage);
        return "";
    }

};

This is the only place I call execute, and it's a button that I'm creating in the onCreate() method. When the task is executed, I get a task already started error.

I thought maybe I'm not cancelling the thread correctly, and so I rebooted my Motrola Xoom. I ran the app once the Xoom was started, and I still got this error. What can I do? The app is using Android SDK 8... and I'm running on a Motorola Xoom.

Upvotes: 0

Views: 1269

Answers (1)

Swayam
Swayam

Reputation: 16364

Make a call to your AsyncTask like this

new LoginTask().execute(""); 

where

private class LoginTask extends
    AsyncTask<Object,String, String> {
.....

}

Upvotes: 2

Related Questions