这个问题在这里已经有答案了:
什么是nullpointerexception,如何修复它(12个答案)
上个月关门了。
基本上,我正在尝试为一个任务片段实现一个回收器视图,并且遵循了大多数人似乎做过的对他们有用的操作,但是遇到了一个问题,myadapter.startlistening()方法会提示以下错误消息:
java.lang.nullpointerexception:尝试对空对象引用调用虚拟方法“void com.example.student\u productivity\u and\u reflection\u app.tasks.taskadapter.startlistening()”
我不确定出了什么问题。这是我的任务类:
public class Task {
private String title, description;
private Date date;
public Task() {
//Empty constructor needed for firebase
}
public Task(String title, String description, Date date) {
this.title = title;
this.description = description;
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
任务适配器:
public class TaskAdapter extends FirestoreRecyclerAdapter <Task, TaskAdapter.TaskHolder>{
public TaskAdapter(@NonNull FirestoreRecyclerOptions<Task> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull TaskHolder holder, int position, @NonNull Task model) {
holder.textViewTitle.setText(model.getTitle());
holder.textViewDescription.setText(model.getDescription());
holder.textViewDate.setText(String.valueOf(model.getDate()));
}
@NonNull
@Override
public TaskHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.task_items, viewGroup, false);
return new TaskHolder(v);
}
class TaskHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewDescription;
TextView textViewDate;
public TaskHolder(@NonNull View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.task_title);
textViewDescription = itemView.findViewById(R.id.task_description);
textViewDate = itemView.findViewById(R.id.task_date);
}
}
}
和任务碎片:
public class TasksFragment extends Fragment{
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private TaskAdapter myAdapter;
private FloatingActionButton newTask;
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tasks, container, false);
}
private void fetchTasks(View view){
Query query = db.collection("users").document("coolguy").collection("task").orderBy("date", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Task> options = new FirestoreRecyclerOptions.Builder<Task>().setQuery(query, Task.class).build();
myAdapter = new TaskAdapter(options);
recyclerView = view.findViewById(R.id.tasksList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(myAdapter);
}
@Override
public void onStart() {
super.onStart();
myAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
myAdapter.stopListening();
}
}
(我知道日期应该是一个具有适当格式的日期变量,但我计划在功能真正起作用后修复它。)
1条答案
按热度按时间cyej8jka1#
你没打电话
fetchTasks()
.你得打电话
fetchTasks()
将视图参数传入onCreateView
返回膨胀视图之前:这就是你需要做的: