我正在尝试创建一个回收站视图,从flickr API中显示Json数据。
获取和解析Json正常工作。
显然我的适配器由于某种原因返回空指针。这是我的片段代码。
public class PhotoGalleryFragment extends Fragment {
private RecyclerView mPhotoRecyclerView;
private static final String TAG = "PhotoGalleryFragment";
private List<GalleryItem> mItems = new ArrayList<>();
public static PhotoGalleryFragment newInstance(){
return new PhotoGalleryFragment();
}
@Override
public void onCreate(Bundle savedInstanceBundle){
super.onCreate(savedInstanceBundle);
setRetainInstance(true);
new FetchItemsTask().execute();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceBundle){
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
RecyclerView mPhotoRecyclerView = (RecyclerView)v.findViewById(R.id.fragment_photo_gallery_recycler_view);
mPhotoRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
setupAdapter();
return v;
}
private void setupAdapter(){
if(isAdded()){
mPhotoRecyclerView.setAdapter(new PhotoAdapter(mItems));
}
}
private class FetchItemsTask extends AsyncTask<Void,Void,List<GalleryItem>>{
@Override
protected List<GalleryItem> doInBackground(Void... params){
return new FlickrFetch().fetchItems();
}
@Override
protected void onPostExecute(List<GalleryItem> items){
mItems= items;
setupAdapter();
}
}
private class PhotoHolder extends RecyclerView.ViewHolder{
private TextView mTitleTextView;
public PhotoHolder(View itemView){
super(itemView);
mTitleTextView = (TextView) itemView;
}
public void bindGalleryItem(GalleryItem item){
mTitleTextView.setText(item.toString());
}
}
private class PhotoAdapter extends RecyclerView.Adapter<PhotoHolder>{
private List<GalleryItem> mGalleryItems;
public PhotoAdapter(List<GalleryItem> galleryItems){
mGalleryItems = galleryItems;
}
@Override
public PhotoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TextView textView = new TextView(getActivity());
return new PhotoHolder(textView);
}
@Override
public void onBindViewHolder(PhotoHolder photoHolder, int position) {
GalleryItem galleryItem = mGalleryItems.get(position);
photoHolder.bindGalleryItem(galleryItem);
}
@Override
public int getItemCount() {
return mGalleryItems.size();
}
}
}
这是我的LogCat输出:
08-19 10:43:24.596 4920-4920/com.bignerdranch.android.photogallery E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bignerdranch.android.photogallery, PID: 4920
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.photogallery/com.bignerdranch.android.photogallery.PhotoGalleryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at com.bignerdranch.android.photogallery.PhotoGalleryFragment.setupAdapter(PhotoGalleryFragment.java:51)
at com.bignerdranch.android.photogallery.PhotoGalleryFragment.onCreateView(PhotoGalleryFragment.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)