简体   繁体   English

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

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

Before posting here ,i have spent considerable time to figure it out myself,i have removed various errors though.在这里发帖之前,我花了相当多的时间自己弄明白,不过我已经删除了各种错误。 I am developing a little notesapp ,evrything worked fine until i used fragments in my app which contains a recycler view.The problem is that i cannot pass the data retrieved from database to recyclerview adapter .我正在开发一个小笔记应用程序,直到我在我的应用程序中使用了包含回收器视图的片段之前,所有东西都运行良好。问题是我无法将从数据库中检索到的数据传递给回收器视图适配器。

the error says "Attempt to invoke virtual method 'void com.example.diary.RecyclerAdapter.setdata(java.util.List)' on a null object reference"错误说“尝试在空对象引用上调用虚拟方法‘void com.example.diary.RecyclerAdapter.setdata(java.util.List)’”

i have verified that the data is not empty ,iam confused why do i get a null pointer error.我已经验证数据不为空,我很困惑为什么我会收到空​​指针错误。

relevant codes are posted below.相关代码张贴如下。

fragment.java片段.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); 'adapter.setdata(notesEntries); //this is where the error occures' //这是错误发生的地方'

adapter.java适配器文件

 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; }

And if i dont use fragments ,this isuue does not arise.Any help is appreciated.如果我不使用片段,则不会出现此问题。感谢任何帮助。

You need to call retrievetasks() method after initializing the adapter.您需要在初始化适配器后调用retrievetasks()方法。 This is causing the problem这导致了问题

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

The adapter in your retrieveTasks is different than the one you initialized. retrieveTasksadapter与您初始化的adapter不同。 You are initializing a local instance, which is outside the scope of your retrieveTasks method.您正在初始化一个本地实例,它超出了您的retrieveTasks方法的范围。 Presumably you have a property in the fragment named adapter already, so you can just delete the local instance to initialize the right one:大概你已经在名为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);
}

Hope that helps!希望有帮助!

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

相关问题 为什么我的 recyclerview 没有在片段中显示数据? - why my recyclerview doesn't show data in fragment? 我想将数据从活动转移到片段,然后在片段中显示数据。 我的代码不起作用 - I want to transfer data from activity to fragment and then display data in the fragment. My code doesn't work 为什么我的水平 recyclerview 不会在片段中滚动 - why my horizantal recyclerview won't scrolling in fragment 为什么我的RecyclerView没有在此片段中显示任何内容? - Why isn't my RecyclerView showing anything in this fragment? 为什么我必须重新启动我的应用程序才能使RecyclerView显示来自SQLite的数据? - Why do I have to restart my app for my RecyclerView to display data from SQLite? 将数据从RecyclerView发送到片段 - Send Data From RecyclerView To Fragment 将数据从 RecyclerView 传递到 Fragment - Passing data from a RecyclerView to Fragment 将数据从 RecyclerView 传递到 Fragment? - Passing data from RecyclerView to Fragment? 无法设置我的 RecyclerView 以显示来自 API 回调的全部数据 - Can't setup my RecyclerView to display whole data from API callback RecyclerView数据为空以在片段中显示textview - RecyclerView data empty to display textview in fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM