AsyncTask的运行ViewPager的每一页上 [英] AsyncTask runs on each page of the ViewPager

本文介绍了AsyncTask的运行ViewPager的每一页上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个选项卡与 Android开发教程

现在我想要做的是非常简单的我用碎片在每一页上。我想告诉每个页面上的RSS提要不同的内容。

Now what I want to do is very simple I use Fragments on each page. I want to show different content from a rss feed on each page.

问题是,当我去到下一个标签运行的AsyncTask(这是在onCreateView)在previous片段。

The problem is when I go to the next tab it runs AsyncTask (which is in onCreateView) of the previous Fragment.

所以,你开始第1页它加载内容的罚款。然后,当你去到第2运行1的片段的onCreateView一次。很显然给出了一个NullException。问题的关键是它不应该在所有的第2页运行第1页AsyncTask的。

So you start on Page 1 it loads the content fine. Then when you go to Page 2 is runs the onCreateView of the Fragment of Page 1 again. And obviously gives an NullException. The point is it should not be running AsyncTask of Page 1 at all at that Page 2.

我不认为有如果是这样告诉我,你需要看哪个部分需要任何例如code。然后,我将编辑我的问题。

I don't think there is any example code needed if so tell me which part you need to see. Then I will edit my question.

AsyncTask的一个ListFragment内:

AsyncTask inside a ListFragment :

public class MyAsyncTask extends AsyncTask<List<String>, Void, List<String>>
{
    // List of messages of the rss feed
    private List<Message> messages;
    private volatile boolean running = true;

    @SuppressWarnings("unused")
    private WeakReference<NieuwsSectionFragment> fragmentWeakRef;

    private MyAsyncTask(NieuwsSectionFragment fragment)
    {
        this.fragmentWeakRef = new WeakReference<NieuwsSectionFragment>(fragment);
    }
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();

        mProgress.show();
       //  progress.setVisibility(View.VISIBLE); //<< set here
    }
    @Override
    protected void onCancelled()
    {
        Log.w("onCancelled", "now cancelled");
        running = false;
    }
    @Override
    protected List<String> doInBackground(List<String>... urls)
    {
        FeedParser parser = FeedParserFactory.getParser();
        messages = parser.parse();
        List<String> titles = new ArrayList<String>(messages.size());
        for (Message msg : messages)
        {
            titles.add(msg.getTitle());
            // Log.w("doInBackground", msg.getTitle());
        }
        return titles;
    }

    @Override
    protected void onPostExecute(List<String> result)
    {
        super.onPostExecute(result);            
        mProgress.dismiss();
        if (result != null)
        {
            PostData data = null;
            listData = new PostData[result.size()];
            for (int i = 0; i < result.size(); i++)
            {
                data = new PostData();                  
                data.postTitle = result.get(i);
                data.postThumbUrl = "http://igo.nl/foto/app_thumb/28991-Taxi-vast-na-poging-tot-nemen-van-sluiproute.jpg";                  
                listData[i] = data;
                Log.w("onPostExecute", "" + listData[i].postTitle);
            }
            adapter = new PostItemAdapter (getActivity(), android.R.layout.simple_list_item_1, listData);               
            setListAdapter(adapter);
            adapter.notifyDataSetChanged(); 
        }
    }
}

这就是所谓的一个方法中和ListFragment的onCreateView内部执行的方法:

It's called inside a method and that method is executed inside the onCreateView of the ListFragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    startNewAsyncTask();
    View rootView = inflater.inflate(R.layout.fragment_section_nieuws, container, false);
    return rootView;
}

@SuppressWarnings("unchecked")
public void startNewAsyncTask()
{       
    MyAsyncTask asyncTask = new MyAsyncTask(this);
    this.asyncTaskWeakRef = new WeakReference<MyAsyncTask>(asyncTask);
    asyncTask.execute();
}

在LogCat中:

The LogCat :

推荐答案

尝试使用 isAdded() onPostExecute() isAdded()如果片段正在增加它的活动返回true。

Try using isAdded() before onPostExecute(). isAdded() returns true if the fragment is currently added to its activity.

http://developer.android.com/reference/安卓/应用/ Fragment.html#isAdded()

@Override
protected void postExecute(){
    if(isAdded()){
        //perform Display Changes
    }
}

这篇关于AsyncTask的运行ViewPager的每一页上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆