Title: My application skips AsyncTask.execute() and crashes
Tags: java;android;android-asynctask
Question: here is my onCreate method the problem is that the app shows the toast "Finish ! " but it dosen't execute ```li.execute(Email,Password);``` and it crashes. that's it
I want the app to do what's inside ```li.execute(Email,Password);``` first.

PS : loginBackgroundWorker is a class that extends ```AsyncTask<String,String,String&gt;``` and it has ```onPreExecute()``` and ```doInBackground()``` and ```onPostExecute()``` methods.
The app dosen't show any errors or warnings but it crashes in the emulator.

here is the code :

```protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    li = new loginBackgroundWorker(LoginActivity.this);
    //loginBackgroundWorker is a class that extends AsyncTask


    EditTextEmail =(EditText) findViewById(R.id.emailField);
    EditTextPassword =(EditText) findViewById(R.id.passwordField);
    loginButton = (CardView) findViewById(R.id.loginButton);

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Email = EditTextEmail.getText().toString().trim();
            Password = EditTextPassword.getText().toString().trim();

            li.execute(Email,Password);
            Toast.makeText(LoginActivity.this,"Finish !",Toast.LENGTH_LONG).show();
        }
    });

}
```

here is AsyncTask Class (I don't think that what is inside those functions is important for you)

```public class loginBackgroundWorker  extends AsyncTask<String,String,String&gt; {
Context context;

public String email;
public String password;

public static final String login_url="http://(779)418-1253/login.php";


public loginBackgroundWorker(Context context) {
    this.context = context;
}


@Override
public void onPreExecute() {
    //Toast.makeText(context,"preExecute",Toast.LENGTH_LONG).show();
}

@Override
public String doInBackground(String... params) {

        try {
            email = params[0];
            password = params[1];
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&amp;"
                    +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();


            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String result="";
            String line="";
            while((line = bufferedReader.readLine())!= null) {
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    return null;
}

@Override
public void onPostExecute(String result) {
    Toast.makeText(context,"Onpost",Toast.LENGTH_LONG).show();
    String r=result.trim();
    Boolean aBoolean = true;
    if (result.equals("1"))
    {
        Toast.makeText(context,"Bien Connecté",Toast.LENGTH_LONG).show();
        aBoolean =  false;
    }
    else if (result.equals(""))
        r = "Login ou mot de passe incorrect";

    if (aBoolean)
    {
        Toast.makeText(context,r,Toast.LENGTH_LONG).show();
    }


}
}
```

here is the StackTrace 

```    --------- beginning of crash
    05-26 14:56:24.023 14756-14756/com.example.zaariou.blacklist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zaariou.blacklist, PID: 14756
java.lang.IllegalStateException: Cannot execute task: the task is already running.
    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:593)
    at android.os.AsyncTask.execute(AsyncTask.java:551)
    at com.example.zaariou.blacklist.LoginActivity$1.onClick(LoginActivity.java:58)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
```

Comment: OK I will show the whole AsyncTask Class

Comment: OK I posted my stacktrace now

Comment: I click the button the first time it shows toast "finish !", when I click it the second time it crashes.

Comment: Show your `AsyncTask` and stacktrade dude

Comment: And what's your exception stacktrace?

Comment: `Cannot execute task: the task is already running.` You're start the task elsewhere or you're probably clicking the button multiple times


Here is another answer: Instance of ```AsyncTask``` can only run once.

If you want to reuse your ```AsyncTask``` you must initialize new instance of your ```loginBackgroundWorker``` and run it.

Move your ```loginBackgroundWorker``` initialization to your ```OnClickListener.onClick``` handle

Comment for this answer: When I did that it crashes after multiple clicks and here is my Stacktrace it shows this error at each click 
E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f811727d2e0

and crashes after that
`--------- beginning of crash
05-26 15:27:05.137 (779)418-1253/com.example.zaariou.blacklist E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.zaariou.blacklist, PID: 20334
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference`

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:05/26/18', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<java><android><android-asynctask>', 'Title': 'My application skips AsyncTask.execute() and crashes', 'ViewCount': 97, 'id': 50543902, 'user_AboutMe': None, 'user_AccountId': 10967772.0, 'user_DisplayName': 'Rachid Zaariou', 'user_Reputation': 57.0, 'user_Views': 19.0, 'user_id': 8059025.0}}