3

My async task call is within the onCreate() method of my activity

my_random_task = new MyRandomTask();
my_random_task.setListener(this);

final Button post_btn = (Button) findViewById(R.id.post_btn);
post_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        EditText post_text = (EditText) findViewById(R.id.post_text);

        if (!post_text.isEmpty()) {
            summoner_search_task.execute(summoner_name);
        }else{
            Toast.makeText(getApplicationContext(), "Please type a post text.", Toast.LENGTH_SHORT).show();
            post_text.requestFocus();
        }

    }
});

The asynctask is empty but when i try to call the asynctask again by pressing the button i get

java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)

Is there any way to make the asynctask reset and be able to run again after i press the button?

stergosz
  • 5,754
  • 13
  • 62
  • 133

1 Answers1

0

I was passing a CallBackListener interface which included a callback function which fired when the async task ended.

So what i did (thanks to @TronicZomB and @stkent) was:

final CallBackListener cbl = this;

final Button post_btn = (Button) findViewById(R.id.post_btn);
post_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        EditText post_text = (EditText) findViewById(R.id.post_text);

        if (!post_text.isEmpty()) {
            my_random_task = new MyRandomTask();
            my_random_task.setListener(cbl);
            summoner_search_task.execute(summoner_name);
        }else{
            Toast.makeText(getApplicationContext(), "Please type a post text.", Toast.LENGTH_SHORT).show();
            post_text.requestFocus();
        }

    }
});
stergosz
  • 5,754
  • 13
  • 62
  • 133