将适配器设置为回收程序视图时出现空指针异常



无法成功呈现回收器视图。收到错误:

"java.lang.NullPointerException: Try to invoke virtual Method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter(' on a null object reference

">

即使我已经设置了启动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对象将在使用前对其进行初始化。

最新更新