问题描述:

Please look into this and let me know what went wrong.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private ItemData[] itemsData;

public MyAdapter(ItemData[] itemsData) {

this.itemsData = itemsData;

}

public MyAdapter(String[] titles, int[] icons, String name, String email, int profile) {}

// Create new views (invoked by the layout manager)

@Override

public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,

View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab1list, null);

// create ViewHolder

ViewHolder viewHolder = new ViewHolder(itemLayoutView);

return viewHolder;

}

// Replace the contents of a view (invoked by the layout manager)

@Override

public void onBindViewHolder(ViewHolder viewHolder, int position) {

// - get data from your itemsData at this position

// - replace the contents of the view with that itemsData

viewHolder.txtViewTitle.setText(itemsData[position].getTitle());

viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());

}

// inner class to hold a reference to each item of RecyclerView

public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView txtViewTitle;

public ImageView imgViewIcon;

public ViewHolder(View itemLayoutView) {

super(itemLayoutView);

txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.abb);

imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.baa);

}

}

// Return the size of your itemsData (invoked by the layout manager)

@Override

public int getItemCount() {

return itemsData.length;// here im getting the `null` pointer exception error

}

}

Null pointer exception at return itemsData.length; i need to design a listview inside a pager or inside a sliding tablayout. but im getting this error.

网友答案:

Your itemsData parameter passed to the MyAdapter's constructor is null or you are using the second constructor which is not initializes the itemsData class member. You can check it for null and return 0 from the getItemCount method:

@Override
public int getItemCount() {
    return itemsData == null ? 0 : itemsData.length;
}
相关阅读:
Top