我想通过名为LOAD MORE的Button在循环触发器中一次又一次地运行名为loadMore的AsyncTask。
错误:
03-07 20:31:38.330:E / AndroidRuntime(504):java.lang.IllegalStateException:无法执行任务:任务已经执行(任务只能执行一次
尝试
onPostExecute()
执行AsyncTask时,按钮LOADMORE才可见。在onPostExecute()
内添加LOADMORE视图。onClickListener
内。onClickListener
。onClickListener LOADMORE按钮代码::
ld. . . . . .loadMore AsyncTask.
ll. . . . . . LinearLayout (where I'm removing and Adding LOADMORE button).
loadMore. . . LOADMORE Button.
public void onClick(View v) {
// TODO Auto-generated method stub
ll.removeView(loadMore);
//loadMore.setVisibility(View.INVISIBLE);
spin2.setVisibility(View.VISIBLE);
if(ld.getStatus()==Status.PENDING)
{
ld.execute();
Log.i("PENDING", "Not yet RUNNING");
}
else if(true)
{
while(ld.getStatus()==Status.RUNNING)
{
Log.i("loadMore Async", "LoadMore RUNNING");
if(ld.getStatus()==Status.FINISHED)
return;
}
ld.execute();
}
}
loadMore AsyncTask ::
的onPostExecute()protected void onPostExecute(Void result) {
adapter = new PhotosAdapter(MainActivity.this, arrPhotos);
gridOfPhotos.setAdapter(adapter);
spin2.setVisibility(View.GONE);
ll.addView(loadMore);
Log.i("loadMore-Async", "Adding LoadMore button again");
Log.i("loadMore-Async", "LoadMore AsyncTask onPost() finished");
loadMore.setOnClickListener(n);
Log.i("loadMore-Async", "Adding Listener to LoadMore");
}
答案 0 :(得分:0)
您无法执行两次AsyncTask。引自文档“任务只能执行一次(如果尝试第二次执行则会抛出异常。)”,链接http://developer.android.com/reference/android/os/AsyncTask.html
除了制作新任务并执行它之外别无选择。
答案 1 :(得分:0)
通过
实现代码::
loadMorePhotos[] lmd=new loadMorePhotos[1000];
LOADMORE按钮的onClickListener ::
View.OnClickListener n=new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ll.removeView(loadMore);
//loadMore.setVisibility(View.INVISIBLE);
spin2.setVisibility(View.VISIBLE);
if(ld.getStatus()==Status.PENDING)
{
ld.execute();
Log.i("loadMoreAsync", "new AsyncTask : 0");
Log.i("PENDING", "Not yet RUNNING");
}
else if(true)
{
Log.i("loadMoreAsync", "Creating new AsyncTask");
lmd[j]=new loadMorePhotos();
lmd[j].execute();
Log.i("loadMoreAsync", "Creating LoadMore AsyncTask :"+j);
j++;
}
}
};