我有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 = "https://m.xsw88.com/allimgs/daicuo/20230907/5808.png.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
}
}