如何在android中实例化多个AsyncTask?

时间:2015-09-28 11:27:48

标签: android android-asynctask classcastexception swiperefreshlayout

我在SwipeRefreshLayout中放入AsyncTask两天后感到沮丧。我已经阅读了SwipeRefreshLayout和Async documnetation,我不知道我做错了什么。串行和并行我都测试过(execute和executeOnexecutor),但没有一个正常工作。 我一直在谷歌搜索,但显然没有其他人处理同样的问题。我究竟做错了什么?请帮帮我!

我有一个ListView活动,我正在尝试添加SwipeRefreshLayout。如果没有滑动布局,每件事情都会顺利进行。但是当我添加滑动时,当它被onRefresh()调用时,我会得到下面的错误。

致命错误:

*.*.*.AllNewsListPageActivity$2 cannot be cast to *.*.*.AllNewsListPageActivity
java.lang.ClassCastException:*.*.AllNewsListPageActivity$2 cannot be cast to *.*.AllNewsListPageActivity
        at **.AllNewsListPageAsync.<init>(AllNewsListPageAsync.java:27)
        at **.AllNewsListPageActivity$2.onRefresh(AllNewsListPageActivity.java:86)
        at android.support.v4.widget.SwipeRefreshLayout$1.onAnimationEnd(SwipeRefreshLayout.java:167)
        at android.support.v4.widget.CircleImageView.onAnimationEnd(CircleImageView.java:107)
        at android.view.ViewGroup.finishAnimatingView(ViewGroup.java:5269)
        at android.view.View.draw(View.java:14407)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
        at android.view.View.draw(View.java:14485)
        at android.view.View.getDisplayList(View.java:13379)
        at android.view.View.getDisplayList(View.java:13421)
        at android.view.View.draw(View.java:14199)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.draw(View.java:14485)
        at android.widget.FrameLayout.draw(FrameLayout.java:472)
        at android.view.View.getDisplayList(View.java:13379)
        at android.view.View.getDisplayList(View.java:13421)
        at android.view.View.draw(View.java:14199)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
        at android.view.View.draw(View.java:14485)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.draw(ActionBarOverlayLayout.java:509)

AllNewsListPageActivity$2结束时 $ 2 是什么?!

以下是代码:

Activity.onCreate()

AllNewsListPageAsync temp = new AllNewsListPageAsync(new Object[]{this, listView});
    temp.execute();//executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);


    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_all_news_list_page_SwipeRefreshLayout);

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            AllNewsListPageAsync temp = new AllNewsListPageAsync(new Object[]{this, listView});
            temp.execute();//executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            mSwipeRefreshLayout.setRefreshing(false);

        }
    });

异步:

public class AllNewsListPageAsync extends AsyncTask<Object, Object, ArrayList<NewsDetailObject>> {


AllNewsListPageActivity context;
ListView listView;
Object[] temp;
String sessionId;
public AllNewsListPageAsync(Object[] params){
    temp = params;
    context = (AllNewsListPageActivity) temp[0];
    listView = (ListView) temp[1];
    sessionId = ((Application9090)context.getApplicationContext()).getSessionId();
}

protected ArrayList<NewsDetailObject> doInBackground(Object[] params) {
    ArrayList<NewsDetailObject> newsList = NewsFeedProxy.getAllNewsDetailObjectList(100, sessionId);
    return newsList;
}

@Override
public void onPostExecute(ArrayList<NewsDetailObject> newsDetailObjectsList){
    super.onPostExecute(newsDetailObjectsList);
    context.newsListIds = ArrayHelper.extractIds(newsDetailObjectsList);

    ((ProgressBar) context.findViewById(R.id.progressBar_activity_all_news_list_page)).setVisibility(View.GONE);

    AllNewsListPageAdapter test;
    test = new AllNewsListPageAdapter(context, R.layout.single_row, R.id.textViewTitleNewsTitle , newsDetailObjectsList);

    listView.setAdapter(test);

    return;
}

非常感谢任何帮助。 谢谢。

2 个答案:

答案 0 :(得分:2)

这里

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        AllNewsListPageAsync temp = new AllNewsListPageAsync(new Object[]{this, listView});
        temp.execute();//executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        mSwipeRefreshLayout.setRefreshing(false);

    }
});

on onRefresh方法内部onRefreshListener类的传递引用 将其更改为:

 mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        AllNewsListPageAsync temp = new AllNewsListPageAsync(new Object[]{AllNewsListPageActivity.this, listView});
        temp.execute();//executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        mSwipeRefreshLayout.setRefreshing(false);

    }
});

答案 1 :(得分:1)

尝试更改

 AllNewsListPageAsync temp = new AllNewsListPageAsync(new Object[]{this, listView});

 AllNewsListPageAsync temp = new AllNewsListPageAsync(new Object[]{AllNewsListPageActivity.this, listView});