2 mhl18820672087 mhl18820672087 于 2017.09.10 22:18 提问

RecyclerView 的onBindViewHolder出现空指针异常

package com.example.haha.note;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**

  • Created by haha on 2017/9/8. */

public class NotesTitleFragment extends Fragment {
private boolean isTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.notes_title_frag,container,false);
RecyclerView noteTitleRecyclerView=(RecyclerView) view.findViewById(R.id.notes_title_recycler_view);
LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
noteTitleRecyclerView.setLayoutManager(layoutManager);
NoteAdapter adapter=new NoteAdapter(getNote());
noteTitleRecyclerView.setAdapter(adapter);
return view;
}
private List getNote(){
List notesList=new ArrayList<>();
for(int i=1;i<=50;i++){
notes note=new notes();
note.setTitle("This is title" + i);
note.setContent(getRandomLengthContent("This is note content"+i+"."));
notesList.add(note);
}
return notesList;
}
private String getRandomLengthContent(String content){
Random random=new Random();
int length=random.nextInt(20)+1;
StringBuilder builder=new StringBuilder();
for(int i=0;i<length;i++){
builder.append(content);
}
return builder.toString();
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.notes_content_layout) !=null){
isTwoPane=true;
}
else isTwoPane=false;

}
class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>{
    private List<notes> mNoteList;
       class ViewHolder extends RecyclerView.ViewHolder{
        TextView noteTitleText;
        public  ViewHolder(View view){
            super(view);
            noteTitleText=(TextView) view.findViewById(R.id.note_title);
        }
    }
    public NoteAdapter(List<notes> notesList){
        mNoteList=notesList;
    }
   @Override
   public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.notes_item,parent,false);
        final ViewHolder holder=new ViewHolder(view);

         view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notes note=mNoteList.get(holder.getAdapterPosition());
                if(isTwoPane){
                    NotesContentFragment notesContentFragment=(NotesContentFragment) getFragmentManager().findFragmentById(R.id.notes_content_fragment);
                   notesContentFragment.refresh(note.getTitle(),note.getContent());
                }else {
                    notesContentActivity.actionStart(getActivity(),note.getTitle(),note.getContent());
                }
            }
        });
        return  holder;
    }

    public void onBindViewHolder(ViewHolder holder,int position){
        notes note=mNoteList.get(position);
        holder.noteTitleText.setText(note.getTitle());
    }

    public int getItemCount(){
        return mNoteList.size();
    }
}

}
看了很多网上得解决办法还是没解决问题,小白求教

3个回答

u1254z62
u1254z62   2017.09.11 09:57

哪行出现空指针???

mhl18820672087
mhl18820672087 public void onBindViewHolder(ViewHolder holder,int position){ notes note=mNoteList.get(position); holder.noteTitleText.setText(note.getTitle()); } holder.noteTitleText.setText(note.getTitle()); //就这行报空指针错误,可是我用 if(holder==null) Log.d("NotesTitleFragment","空"); else Log.d("NotesTitleFragment",note.getTitle()); } 日志打出了note里的值,那就说明holder和note都不为空,请大神解释下为什么
2 个月之前 回复
dongfeng9ge
dongfeng9ge   Ds   Rxr 2017.09.14 20:57

你把logcat贴上来

mhl18820672087
mhl18820672087   2017.10.14 21:50

来吃了好久,问题已经解决了!原来是我有个相似ID的控件,然后引错了控件

Csdn user default icon
上传中...
上传图片
插入图片