java - 将适配器设置为回收器视图时出现空指针异常
问题描述
无法成功呈现回收站视图。得到错误:
“java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)'”
即使我设置了初始化RecyclerView
,设置线性布局并将其设置为适配器。谁能指出错误?
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
if(getIntent() != null ){
if(getIntent().getStringExtra("categoryId") != null){
categoryId = getIntent().getStringExtra("categoryId");
fetchFoodList(categoryId);
}
}
databaseReference = FirebaseDatabase.getInstance().getReference("foodlist");
floatingActionButton = findViewById(R.id.food_list_fab);
recyclerView = findViewById(R.id.food_recycler_view);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
}
private void fetchFoodList(String categoryId) {
Query query = FirebaseDatabase.getInstance().getReference("foodlist").orderByChild("MenuId").equalTo(categoryId);
FirebaseRecyclerOptions<FoodModel> options = new FirebaseRecyclerOptions.Builder<FoodModel>().setQuery(query, new SnapshotParser<FoodModel>() {
@NonNull
@Override
public FoodModel parseSnapshot(@NonNull DataSnapshot snapshot) {
Log.d(TAG, "parseSnapshot: "+snapshot.child("Discount").getValue().toString());
return new FoodModel(snapshot.child("Description").getValue().toString(), snapshot.child("Discount").getValue().toString(), snapshot.child("Image").getValue().toString(), snapshot.child("MenuId").getValue().toString(), snapshot.child("Name").getValue().toString(), snapshot.child("Price").getValue().toString());
}
}).build();
FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<FoodModel, FoodViewHolder>(options) {
@NonNull
@Override
public FoodViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View v = inflater.inflate(R.layout.food_items, viewGroup, false);
return new FoodViewHolder(v);
}
@Override
protected void onBindViewHolder(@NonNull FoodViewHolder holder, final int position, @NonNull FoodModel model) {
holder.setFoodName(model.getName());
Log.d(TAG, "onBindViewHolder: "+model.getImage());
holder.setFoodImage(model.getImage());
holder.setFoodDescription(model.getDescription());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
解决方案
要解决此问题,请移动以下代码行:
recyclerView = findViewById(R.id.food_recycler_view);
在这一行之后:
searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);.
这样,RecyclerView
对象将在使用前被初始化。
推荐阅读
- wordpress - 特色图片始终显示在自定义帖子模板中
- javascript - 在javascript中按字母顺序对数组及其对象键进行排序
- ruby - 如何使用正则表达式从字符串中提取域部分
- java - 在 Spring Boot 中具有 jar 文件之外的配置文件
- icons - 我在哪里可以找到所有垫子图标的列表——Angular
- c# - 带有来自父数据集的子报告的 RDLC 报告?
- c# - 如何使用包含“。”的键反序列化 json 在 C# 中?
- php - 从 instagram hashtag 中获取数据属性
- html - 在标题中嵌入 Bootstrap 3 导航栏
- dask - Dask数据框获得第二高的值和列名