Java 为什么是';t我的片段无法显示来自recyclerview的数据?
在这里发布之前,我花了相当长的时间自己去弄清楚,不过我已经消除了各种错误。我正在开发一个小notesapp,在我的应用程序中使用包含回收器视图的片段之前,evrything工作得很好。问题是我无法将从数据库检索到的数据传递给recyclerview适配器 错误表示“试图在空对象引用上调用虚拟方法'void com.example.diary.RecyclerAdapter.setdata(java.util.List)'” 我已经验证了数据不是空的,我不明白为什么会出现空指针错误 相关代码如下所示 fragment.javaJava 为什么是';t我的片段无法显示来自recyclerview的数据?,java,android,android-fragments,android-recyclerview,Java,Android,Android Fragments,Android Recyclerview,在这里发布之前,我花了相当长的时间自己去弄清楚,不过我已经消除了各种错误。我正在开发一个小notesapp,在我的应用程序中使用包含回收器视图的片段之前,evrything工作得很好。问题是我无法将从数据库检索到的数据传递给recyclerview适配器 错误表示“试图在空对象引用上调用虚拟方法'void com.example.diary.RecyclerAdapter.setdata(java.util.List)'” 我已经验证了数据不是空的,我不明白为什么会出现空指针错误 相关代码如下所
@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 }
});
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; }
@覆盖
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u recents,container,false);
回收适配器;
db=NotesDatabase.createdb(getContext());
适配器=新的回收适配器(numofitems);
检索任务();
layoutManager=新的LinearLayoutManager(getActivity());
binding=DataBindingUtil.setContentView(getActivity(),R.layout.fragment\u recents);
binding.recyclerView.setLayoutManager(layoutManager);
binding.recyclerView.setAdapter(适配器);
Log.d(“adapeter”,“已设置”);
返回视图;
}
公共无效检索任务(){
试一试{
LiveData notesEntries=db.Dao().loadalltasks();
注意哨兵。观察(这个,新观察者){
@凌驾
更改后的公共无效(列表注释哨兵){
注释=注释哨兵;
Log.d(“sizeforall”,String.valueOf(notesEntries.size());
adapter.setdata(notesEntries);//这是发生错误的地方}
});
'adapter.setdata(notesEntries);//这是发生错误的地方'
adapter.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 }
});
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; }
public void setdata(列表注释sentries){
试一试{
notesEntryList=notesEntries;
notifyDataSetChanged();
}捕获(例外e){
e、 printStackTrace();
}
}
公共列表getnotesentries(){
返回注释Sentrylist;
}
@凌驾
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder,int位置){
试一试{
NotesEntry notesEntries=notesEntryList.get(位置);
id=notesEntries.getId();
text=notesEntries.getText();
//b1.setText(String.valueOf(id));
textview1.setText(文本);
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
public int getItemCount(){
如果(!notesEntryList.isEmpty()){
Log.d(“sizereturned”,String.valueOf(notesEntryList.size());
return notesEntryList.size();
}否则
Log.d(“sizereturned1”,String.valueOf(notesEntryList.size());
返回0;}
如果我不使用片段,则不会出现此isuue。非常感谢您的帮助。初始化适配器后,您需要调用
retrievetasks()
方法。这是导致问题的原因
adapter = new RecyclerAdapter(numofitems);
retrievetasks();
retrieveTasks
中的适配器
与您初始化的适配器不同。您正在初始化一个本地实例,该实例不在retrieveTasks
方法的范围内。您可能已经在名为adapter
的片段中有一个属性,因此您可以将本地实例删除到initia请选择正确的一个:
@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);
}
@覆盖
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u recents,container,false);
RecyclerAdapter;//谢谢您的回答。我以前尝试过,现在也尝试过,结果没有改变。相同的空指针错误。@Viswanath Kumar SanduRecyclerAdapter;将此行移到onCreateView方法之外,您在哪里初始化numofitems
?