适配器中的空指针异常,用于getItemsCount()中的recyclerview

时间:2016-02-03 13:45:51

标签: android android-fragments android-recyclerview

我正在尝试使用片段实现recyclerview。但我总是得到这个错误我=我无法弄明白。任何帮助表示赞赏。

这是我想要列入列表的数据的代码:

public class ItemData {
public int BatImageUrl;
public String BatTitle;


public int getBatImageUrl() {
    return BatImageUrl;
}

public void setBatImageUrl(int batImageUrl) {
    BatImageUrl = batImageUrl;
}

public String getBatTitle() {
    return BatTitle;
}

public void setBatTitle(String batTitle) {
    BatTitle = batTitle;
}

public ItemData(int BatImageUrl,String BatTitle){
    this.BatImageUrl=BatImageUrl;
    this.BatTitle=BatTitle;
}
public List<ItemData> itemsData;
public void initializeData(){
    itemsData = new ArrayList<>();
    itemsData.add(new ItemData(R.drawable.batman2,"batman-1"));
    itemsData.add(new ItemData(R.drawable.batman,"batman-2"));
    itemsData.add(new ItemData(R.drawable.batman3,"batman-3"));

}

}

这是我的适配器代码:

   public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private List<ItemData> itemsData;

    public MyAdapter(List<ItemData> itemData) {
        this.itemsData = itemData;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View BatmanView = LayoutInflater.from(parent.getContext()).inflate(R.layout.batman_cards,parent,false);

        ViewHolder viewHolder = new ViewHolder(BatmanView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        ItemData id = itemsData.get(position);
        viewHolder.BatImage.setImageResource(id.getBatImageUrl());
        viewHolder.BatTitle.setText(id.getBatTitle());

    }



    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView BatImage;
        TextView BatTitle;
        CardView cv;
        public ViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.bat_card);
             BatImage = (ImageView) itemView.findViewById(R.id.bat_image);
            BatTitle = (TextView) itemView.findViewById(R.id.bat_title);



        }
    }
    @Override
    public int getItemCount() {
        return itemsData.size();
    }
}

这是我的片段代码:

  public class BatmanFragment extends Fragment {


    List<ItemData> itemsData;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View BatmanView = inflater.inflate(R.layout.batman_fragment,container,false);

        return BatmanView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RecyclerView batmanRecyclerview = (RecyclerView) getView().findViewById(R.id.batman_recycler_view);
        batmanRecyclerview.setHasFixedSize(true);
       GridLayoutManager gm = new GridLayoutManager(getActivity(),2);
        gm.setOrientation(GridLayoutManager.VERTICAL);
        batmanRecyclerview.setLayoutManager(gm);


        MyAdapter mAdapter = new MyAdapter(itemsData);
        batmanRecyclerview.setAdapter(mAdapter);
        batmanRecyclerview.setItemAnimator(new DefaultItemAnimator());
    }
}

最后我的主要活动:

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BatmanFragment fragmentS1 = new BatmanFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.your_placeholder, fragmentS1).commit();
    }
}

3 个答案:

答案 0 :(得分:0)

你的itemsData为null所以它给你一个NPE更改getItemCount()

@Override
    public int getItemCount() {
        return itemsData.size();
    }

@Override
    public int getItemCount() {
if(itemsData==null)
return 0;
        return itemsData.size();
    }

或者你可能错过了初始化itemsData

答案 1 :(得分:0)

您没有初始化ArrayList

像这样改变你的方法

public List<ItemData initializeData(){
    itemsData = new ArrayList<>();
    itemsData.add(new ItemData(R.drawable.batman2,"batman-1"));
    itemsData.add(new ItemData(R.drawable.batman,"batman-2"));
    itemsData.add(new ItemData(R.drawable.batman3,"batman-3"));

    return itemsData;
}

添加此

itemsData = new ItemData().initializeData();

MyAdapter mAdapter = new MyAdapter(itemsData)

<强>更新

像这样向ItemData类添加默认构造函数。

public ItemData(int BatImageUrl,String BatTitle){

}

答案 2 :(得分:0)

你必须初始化itemData = new ArrayList&lt;&gt;();在此之前 MyAdapter mAdapter = new MyAdapter(itemsData);