我有一个Indeterminate进度条替换我的MenuItem刷新按钮,而AsyncTask加载数据。所有这些都没有问题:
我的创建选项菜单:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.videos, menu);
menuItem = menu.findItem(R.id.button_refresh);
super.onCreateOptionsMenu(menu, inflater);
}
异步任务:
class ReviewLoaderAsyncTask extends AsyncTask<Void, Void, ArrayList<Review>> implements Api {
private Context context;
private int offset;
private FragmentCallback mFragmentCallback;
ArrayList<Review> add;
public ReviewLoaderAsyncTask(Context _context, int _offset, FragmentCallback _mFragmentCallback)
{
context = _context;
offset = _offset;
mFragmentCallback = _mFragmentCallback;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
menuItem.setActionView(R.layout.progressbar); //ERROR OCCURS HERE
menuItem.expandActionView();
}
@Override
protected ArrayList<Review> doInBackground(Void... voids) {
// Call some data here
return add;
}
@Override
protected void onPostExecute(ArrayList<Review> _reviews) {
super.onPostExecute(_reviews);
menuItem.collapseActionView();
menuItem.setActionView(null);
mFragmentCallback.taskIsFinished(_reviews, offset);
}
}
一切正常。但是,我想在创建视图后立即开始加载此数据。我在onCreateView部分
中执行此操作@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.review_fragment_gridview, container, false);
mGridView = (StaggeredGridView) view.findViewById(R.id.myGrid);
reviews = new ArrayList<Review>();
offset = 0;
ReviewLoaderAsyncTask loadRSSTask = new ReviewLoaderAsyncTask(getActivity(), offset, new FragmentCallback() {
@Override
public void taskIsFinished(ArrayList<Review> add, int _offset) {
for (Review v : add) {
reviews.add(v);
}
mReviewsAdapter = new ReviewsAdapter(getActivity(), reviews, getFragmentManager());
offset = _offset;
mGridView.setAdapter(mReviewsAdapter);
}
});
if(savedInstanceState == null){
loadRSSTask.execute();
} else{
reviews = savedInstanceState.getParcelableArrayList(REVIEWS_LIST_TAG);
ReviewsAdapter adapter = new ReviewsAdapter(getActivity(), reviews, getFragmentManager());
mGridView.setAdapter(adapter);
((BaseAdapter) mGridView.getAdapter()).notifyDataSetChanged();
}
return view;
}
我遇到的问题是AsyncTask在菜单膨胀之前启动。当我到达AsyncTask的PreExecute部分时,这会导致NullPointerException。
是否有更好的实现方法可以在onCreateView中开始数据加载并仍然保留我的ProgressBar动作视图?
答案 0 :(得分:0)
根据cYrixmorten的反馈,我将Async执行调用移到了onCreateOptionsMenu。这是修改后的代码。
修改onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.review_fragment_gridview, container, false);
mGridView = (StaggeredGridView) view.findViewById(R.id.myGrid);
bundle = savedInstanceState;
reviews = new ArrayList<Review>();
offset = 0;
ReviewLoaderAsyncTask loadRSSTask = new ReviewLoaderAsyncTask(getActivity(), offset, new FragmentCallback() {
@Override
public void taskIsFinished(ArrayList<Review> add, int _offset) {
for (Review v : add) {
reviews.add(v);
}
System.out.println(reviews.size() +" is the video size!");
mReviewsAdapter = new ReviewsAdapter(getActivity(), reviews, getFragmentManager());
offset = _offset;
mGridView.setAdapter(mReviewsAdapter);
}
});
if(savedInstanceState!=null){
reviews = savedInstanceState.getParcelableArrayList(REVIEWS_LIST_TAG);
ReviewsAdapter adapter = new ReviewsAdapter(getActivity(), reviews, getFragmentManager());
mGridView.setAdapter(adapter);
((BaseAdapter) mGridView.getAdapter()).notifyDataSetChanged();
}
return view;
}
修改onCreateOptionsMenu:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.videos, menu);
menuItem = menu.findItem(R.id.button_refresh);
ReviewLoaderAsyncTask loadRSSTask = new ReviewLoaderAsyncTask(getActivity(), offset, new FragmentCallback() {
@Override
public void taskIsFinished(ArrayList<Review> add, int _offset) {
System.out.println(add.size() + "is the passed array size to the callback");
for (Review v : add) {
reviews.add(v);
}
mReviewsAdapter = new ReviewsAdapter(getActivity(), reviews, getFragmentManager());
offset = _offset;
mGridView.setAdapter(mReviewsAdapter);
}
});
if(bundle == null){
loadRSSTask.execute();
}
super.onCreateOptionsMenu(menu, inflater);
}
这是有效的,但我不会将其标记为现在的解决方案,希望能够为这个问题推广更多讨论和替代解决方案。