[英]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. retrieveTasks
的adapter
与您初始化的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.