我最近将代码从活动转移到片段,但是在尝试在片段内调用回收器视图适配器的公共方法“ attachDatabaseReadListener()”时遇到此错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.digicube.RecyclerViewAdapter.attachDatabaseReadListener()' on a null object reference
在转到片段之前,我可以通过调用adapter.publicMethod()
之类的适配器来调用适配器的公共方法,但是它现在在片段中不起作用。
这是我的RecyclerView适配器的代码:
import ...
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
...
public RecyclerViewAdapter(Callback callback, Context context) {
mCallback = callback;
this.mContext = context;
// ArrayList to hold task objects from Firebase database for recyclerViewAdapter
mBrands = new ArrayList<>();
// Initialize Firebase Components
mFirebaseDatabase = FirebaseDatabase.getInstance();
mBrandsDatabaseReference = mFirebaseDatabase.getReference().child("brands");
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.brand_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final TaskStructure taskStructure = mBrands.get(position);
holder.mBrandNameTextView.setText(taskStructure.getBrandName());
holder.mCategoryTextView.setText(taskStructure.getCategory());
holder.mAvgUserRatingTextView.setText(String.valueOf(taskStructure.getAvgUserRating()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
...
}
});
}
@Override
public int getItemCount() {
Collections.sort(mBrands, TaskStructure.AverageUserRatingComparator);
return mBrands.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView mBrandNameTextView;
private TextView mCategoryTextView;
private TextView mAvgUserRatingTextView;
private ImageView mBrandImageView;
public ViewHolder(final View itemView) {
super(itemView);
mBrandNameTextView = itemView.findViewById(R.id.brand_name);
mCategoryTextView = itemView.findViewById(R.id.category);
mAvgUserRatingTextView = itemView.findViewById(R.id.avg_user_rating);
mBrandImageView = itemView.findViewById(R.id.brand_image);
}
}
public void attachDatabaseReadListener() {
if (mChildEventListener == null) {
mChildEventListener = new ChildEventListener() {
...
};
mBrandsDatabaseReference.addChildEventListener(mChildEventListener);
}
}
}
我正在片段的onViewCreated方法上启动它:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
recyclerViewBrands = view.findViewById(R.id.recycler_view_brands);
recyclerViewBrands.setLayoutManager(new LinearLayoutManager(this.getActivity()));
brandsAdapter = new com.example.digicube.RecyclerViewAdapter(this, getActivity());
recyclerViewBrands.setAdapter(brandsAdapter);
}
// Method call by activity on signed in
public void attachListener(){
brandsAdapter.attachDatabaseReadListener();
}
答案 0 :(得分:0)
通过在片段的onViewCreated()方法末尾调用attachDatabaseReadListener()
方法来使其工作。
以前,我是在公共方法attachListener()
上调用它的,该方法在onViewCreated()之前由活动执行,导致错误!
感谢Mike M.的评论,这有助于我理解问题。