我有一个gridview来显示来自网址的图片。我想用this library添加pull-to-refresh功能。但是,我收到错误,我不知道logcat输出说什么。你能帮我解决这个错误吗?
这是我的活动类,它会产生错误。
public class ImageGridFragment extends AbsListViewBaseFragment {
public static final int INDEX = 1;
DisplayImageOptions options;
private PullToRefreshGridView mPullRefreshGridView;
List<String> imageUrls = getImages.IMAGE;
GridView mGridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.gridview_main, container, false);
mPullRefreshGridView = (PullToRefreshGridView) rootView.findViewById(R.id.gridview_);
mGridView = mPullRefreshGridView.getRefreshableView();
mGridView.setAdapter(new ImageAdapter());
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
mGridView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
startAddCollectionActivity(position);
return true;
}
});
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
Toast.makeText(getActivity(), "Pull Down!", Toast.LENGTH_SHORT).show();
new GetDataTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
Toast.makeText(getActivity(), "Pull Up!", Toast.LENGTH_SHORT).show();
new GetDataTask().execute();
}
});
return rootView;
}
private List<String> getData() {
return imageUrls;
}
private class GetDataTask extends AsyncTask<Void, Void, List<String>> {
@Override
protected List<String> doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return imageUrls;
}
@Override
protected void onPostExecute(List<String> result) {
imageUrls.addAll(getData());
((BaseAdapter) mGridView.getAdapter()).notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshGridView.onRefreshComplete();
super.onPostExecute(result);
}
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater inflater;
ImageAdapter() {
inflater = LayoutInflater.from(getActivity());
}
@Override
public int getCount() {
return imageUrls.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ImageLoader.getInstance()
.displayImage(imageUrls.get(position), holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
return view;
}
}
static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}
这是log cat输出:
02-08 21:17:37.953: E/AndroidRuntime(2495): FATAL EXCEPTION: main
02-08 21:17:37.953: E/AndroidRuntime(2495): java.lang.NullPointerException
02-08 21:17:37.953: E/AndroidRuntime(2495): at com.example.imageLoaderLibrary.ImageGridFragment.onCreateView(ImageGridFragment.java:78)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-08 21:17:37.953: E/AndroidRuntime(2495): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-08 21:17:37.953: E/AndroidRuntime(2495): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.View.measure(View.java:15172)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-08 21:17:37.953: E/AndroidRuntime(2495): at android.view.Choreographer.doFrame(Choreographer.java:525)
修改:此行收到错误:mPullRefreshGridView = (PullToRefreshGridView) rootView.findViewById(R.id.gridview_);
答案 0 :(得分:0)
onCreateView
函数第78行出错 - 只有你知道哪一行。
但我会猜测并说这条线可能会失败:
mPullRefreshGridView = (PullToRefreshGridView) rootView.findViewById(R.id.gridview_);