注意应用:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

Note App: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

提问人:Revaken 提问时间:3/19/2023 最后编辑:MikeTRevaken 更新时间:3/19/2023 访问量:24

问:

我正在创建一个记事本应用程序,但是,此错误发生在

startup:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference. 
In the log, the program points to these lines:at com.example.tasktrackingandmanagment.adapters.MyNoteAdapter$ViewHolder.setNote(MyNoteAdapter.java:68)
                                                                                                    at com.example.tasktrackingandmanagment.adapters.MyNoteAdapter.onBindViewHolder(MyNoteAdapter.java:36)
                                                                                                    at com.example.tasktrackingandmanagment.adapters.MyNoteAdapter.onBindViewHolder(MyNoteAdapter.java:20)

代码如下:

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

    List<MyNoteEntities> noteEntitiesList;

    public MyNoteAdapter(List<MyNoteEntities> noteEntitiesList) {
        this.noteEntitiesList = noteEntitiesList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.setNote(noteEntitiesList.get(position));

    }

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

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView title,textNote,dateTime;
        private LinearLayout linearLayout;
        RoundedImageView roundedImageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            title = itemView.findViewById(R.id.textTitle);
            textNote = itemView.findViewById(R.id.textNote);
            linearLayout = itemView.findViewById(R.id.layoutNote);
            roundedImageView = itemView.findViewById(R.id.imageNote_item);
        }

        public void setNote(MyNoteEntities noteEntities) {
            title.setText(noteEntities.getTitle());
            textNote.setText(noteEntities.getNoteText());
            dateTime.setText(noteEntities.getDateTime());

            GradientDrawable gradientDrawable = (GradientDrawable) linearLayout.getBackground();
            if (noteEntities.getColor() != null) {
                gradientDrawable.setColor(Color.parseColor(noteEntities.getColor()));
            }
            else{
                gradientDrawable.setColor(Color.parseColor("#FF937B"));
            }
        }
    }
}

java null nullpointerexception android-room settext

评论


答: 暂无答案