簡體   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