繁体   English   中英

为什么我的片段不能显示来自 recyclerview 的数据?

[英]Why ain't my fragment cannot display data from recyclerview?

在这里发帖之前,我花了相当多的时间自己弄明白,不过我已经删除了各种错误。 我正在开发一个小笔记应用程序,直到我在我的应用程序中使用了包含回收器视图的片段之前,所有东西都运行良好。问题是我无法将从数据库中检索到的数据传递给回收器视图适配器。

错误说“尝试在空对象引用上调用虚拟方法‘void com.example.diary.RecyclerAdapter.setdata(java.util.List)’”

我已经验证数据不为空,我很困惑为什么我会收到空​​指针错误。

相关代码张贴如下。

片段.java

 @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_recents, container, false);
    RecyclerAdapter adapter;
    db = NotesDatabase.createdb(getContext());

    adapter = new RecyclerAdapter(numofitems); 
     retrievetasks();
    layoutManager = new LinearLayoutManager(getActivity());
    binding = DataBindingUtil.setContentView(getActivity(), R.layout.fragment_recents);
    binding.recyclerView.setLayoutManager(layoutManager);
    binding.recyclerView.setAdapter(adapter);
    Log.d("adapeter", "has set");
    return view;
}

public void retrievetasks() {
    try {
        LiveData<List<NotesEntry>> notesEntries = db.Dao().loadalltasks();
        notesEntries.observe(this, new Observer<List<NotesEntry>>() {
            @Override
            public void onChanged(List<NotesEntry> notesEntries) {
                notes = notesEntries;
                Log.d("sizeforall", String.valueOf(notesEntries.size()));
                adapter.setdata(notesEntries); //this is where the error occures }
             });

'adapter.setdata(notesEntries); //这是错误发生的地方'

适配器文件

 public void setdata(List<NotesEntry> notesEntries) {
    try {
        notesEntryList = notesEntries;
        notifyDataSetChanged();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public List<NotesEntry> getnotesentries() {
    return notesEntryList;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    try {
        NotesEntry notesEntries = notesEntryList.get(position);
        id = notesEntries.getId();
        text = notesEntries.getText();
        // b1.setText(String.valueOf(id));
        textview1.setText(text);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public int getItemCount() {
    if (!notesEntryList.isEmpty()) {
        Log.d("sizereturned", String.valueOf(notesEntryList.size()));
        return notesEntryList.size();
    } else
        Log.d("sizereturned1", String.valueOf(notesEntryList.size()));
    return 0; }

如果我不使用片段,则不会出现此问题。感谢任何帮助。

您需要在初始化适配器后调用retrievetasks()方法。 这导致了问题

adapter = new RecyclerAdapter(numofitems);
retrievetasks();

retrieveTasksadapter与您初始化的adapter不同。 您正在初始化一个本地实例,它超出了您的retrieveTasks方法的范围。 大概你已经在名为adapter的片段中有一个属性,所以你可以删除本地实例来初始化正确的实例:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_recents, container, false);
    RecyclerAdapter adapter; // <- DELETE THIS
    db = NotesDatabase.createdb(getContext());
    adapter = new RecyclerAdapter(numofitems);
}

希望有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM