使用Retrofit使用json并使用非同步回调后,我无法将MainList中的An ArrayList传递给片段。
MainActivity的代码:
lFragmentManager = getFragmentManager();
lFragment = lFragmentManager.findFragmentById(R.id.frame_container);
lFragment = new Fragment_Categories();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("list_categories", Categories_.getCategories());
//for(int a = 0 ; a < Categories_.getCategories().size(); a++)
// Log.d("billy",Categories_.getCategories().get(a).getTitle());
lFragment.setArguments(bundle);
lFragmentManager.beginTransaction().replace(R.id.frame_container ,lFragment ).commit();
请注意,Log inside注释会打印列表的上下文,因此我会使用此代码在ArrayList
中获取片段中的onCreateView
:
if(savedInstanceState != null)
categories = savedInstanceState.getParcelableArrayList("list_categories");
/*
* initialize the Recycler view
*/
mRecycler = (RecyclerView)view.findViewById(R.id.categories_list);
mAdapter = new AdapterCategories(categories,getActivity());
mRecycler.setAdapter(mAdapter);
这是我的班级类别:
public class Categories implements Parcelable{
private ArrayList<NavDrawerItem> categories;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(categories);
}
public static final Parcelable.Creator<Categories> CREATOR = new Parcelable.Creator<Categories>() {
public Categories createFromParcel(Parcel in) {
return new Categories(in);
}
public Categories[] newArray(int size){
return new Categories[size];
}
};
private Categories(Parcel in){
categories = in.createTypedArrayList(NavDrawerItem.CREATOR);
}
public ArrayList<NavDrawerItem> getCategories() {
return categories;
}
}
这是Class NavDrawerItem:
public class NavDrawerItem implements Parcelable {
private String title;
private String description;
private String image;
private String post_count;
private String id;
private String slug;
private int parent;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(description);
dest.writeString(image);
dest.writeString(post_count);
dest.writeString(id);
dest.writeString(slug);
dest.writeInt(parent);
}
public static final Parcelable.Creator<NavDrawerItem> CREATOR = new Parcelable.Creator<NavDrawerItem>() {
public NavDrawerItem createFromParcel(Parcel in) {
return new NavDrawerItem(in);
}
public NavDrawerItem[] newArray(int size){
return new NavDrawerItem[size];
}
};
private NavDrawerItem(Parcel in){
title = in.readString();
description = in.readString();
image = in.readString();
post_count = in.readString();
id = in.readString();
slug = in.readString();
parent = in.readInt();
}
问题是内部片段(当试图将列表传递给RecyclerView
的适配器时)或RecyclerView
的适配器内部时,我采用了一个空例外:
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.util.ArrayList android.os.Bundle.getParcelableArrayList(java.lang.String)'
感谢您的帮助!!
答案 0 :(得分:2)
您不会使用savedInstanceState
来获取您的参数,因为它们不是作为saveInstanceState()
操作的一部分传递的。您想要使用getArguments()
。所以试试这个......
categories = getArguments().getParcelableArrayList("list_categories");