我的异步任务调用在我的活动的onCreate()方法中
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();
}
}
});
asynctask是空的,但当我尝试按下按钮时再次调用asynctask
java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
有没有办法让asynctask重置并在按下按钮后再次运行?
答案 0 :(得分:0)
我正在传递一个CallBackListener接口,该接口包含一个回调函数,该函数在异步任务结束时触发。
所以我所做的(感谢@TronicZomB和@stkent)是:
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();
}
}
});