我已经为recyclerview设置了布局管理器,但我仍然遇到错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure
具有recyclerview的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_blank, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.gridContainer);
RecyclerView.LayoutManager l = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(l);
recyclicAdapter = new RecyclicAdapter(getActivity(),getData());
recyclerView.setAdapter(recyclicAdapter);
return layout;
}
请仔细研究一下,让我知道出了什么问题。
我在下面发布了适配器代码作为答案。
答案 0 :(得分:0)
Adaper Code:
将数据写入recyclerview的适配器:
private LayoutInflater inflater;
List<String> arrayList = Collections.emptyList();
RecyclicAdapter(Context context,List<String> data){
inflater = LayoutInflater.from(context);
this.arrayList = data;
}
@Override
public KViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.sinrow,parent,false);
KViewHolder holder = new KViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(KViewHolder holder, int position) {
holder.textView.setText(arrayList.get(position));
}
@Override
public int getItemCount() {return arrayList.size();}
class KViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public KViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.passtext);
}
}
答案 1 :(得分:0)
使用此代码
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);