Java 如何修复空光标-android studio
我真的非常感谢你的帮助!我正在做一个项目,我被困在这一部分 日志类别:Java 如何修复空光标-android studio,java,sqlite,android-studio,android-layout,Java,Sqlite,Android Studio,Android Layout,我真的非常感谢你的帮助!我正在做一个项目,我被困在这一部分 日志类别: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndex(java.lang.String)' on a null object reference at com.example.workhours.NotesCustomAdapter.onBindViewHol
java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndex(java.lang.String)' on a null object reference
at com.example.workhours.NotesCustomAdapter.onBindViewHolder(NotesCustomAdapter.java:38)
at com.example.workhours.NotesCustomAdapter.onBindViewHolder(NotesCustomAdapter.java:16)
代码:
16)公共类NotesCustomAdapter扩展了RecyclerView.Adapter{
17) 私有ArrayList ArrayList注释;
18) 私人语境;
19) 光标;
20) public NotesCustomAdapter(ArrayList arrayListNote,上下文){
21)this.arrayListNote=arrayListNote;
22)this.context=上下文;
23) }
35)@超控
36)BindViewHolder上的公共无效(@NonNull ViewHolder,int位置){
37)holder.notePadTextView.setText(arrayListNote.get(position.getNote());
38)long id=cursor.getLong(cursor.getColumnIndex(DataBaseHelper.COL_0));
39)holder.itemView.setTag(id);
40) }
您有游标对象,但您没有像对arrayListNote和context那样初始化此对象,因此您有空对象引用。谢谢您!但是如何初始化游标?您可以查看这些链接。对不起,我真的不知道如何在我自己的代码中实现它
16) public class NotesCustomAdapter extends RecyclerView.Adapter<NotesCustomAdapter.ViewHolder>{
17) private ArrayList<newNote> arrayListNote;
18) private Context context;
19) Cursor cursor;
20) public NotesCustomAdapter(ArrayList<newNote> arrayListNote, Context context) {
21) this.arrayListNote = arrayListNote;
22) this.context = context;
23) }
35) @Override
36) public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
37) holder.notePadTextView.setText(arrayListNote.get(position).getNote());
38) long id = cursor.getLong(cursor.getColumnIndex(DataBaseHelper.COL_0));
39) holder.itemView.setTag(id);
40) }