У меня есть эта странная проблема, которая никогда не возникала у меня ни в одном из моих приложений для пожарного магазина, у меня есть это Recyclerview, где я хочу отобразить изображение и текст из своего пожарного магазина, и я получаю следующую ошибку.
java.lang.NullPointerException: попытка вызвать виртуальный метод 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges ()' для ссылки на пустой объект
Этокак мои данные в firestore присутствуют:
Ниже приведен мой класс активности
package rimapps.appslet.lchfmalayalam.view;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.google.firebase.FirebaseApp;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
import rimapps.appslet.lchfmalayalam.R;
import rimapps.appslet.lchfmalayalam.adapter.BreakfastAdapter;
import rimapps.appslet.lchfmalayalam.model.ContentsBreakfast;
public class BreakfastActivity extends AppCompatActivity {
private FirebaseFirestore db;
private static final String TAG="BreakfastActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FirebaseApp.initializeApp(this);
setContentView(R.layout.activity_breakfast);
RecyclerView recbrf = (RecyclerView)findViewById(R.id.rec_brf);
recbrf.setNestedScrollingEnabled(false);
final List<ContentsBreakfast> breakfastList = new ArrayList();
recbrf.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
final BreakfastAdapter adapterb = new BreakfastAdapter(breakfastList, getApplicationContext());
recbrf.setAdapter(adapterb);
db = FirebaseFirestore.getInstance();
db.collection("breakfast").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e!=null){
Log.d(TAG,"Error:"+e.getMessage());
}
for (DocumentChange doc: documentSnapshots.getDocumentChanges()){
if (doc.getType() == DocumentChange.Type.ADDED){
//String title = doc.getDocument().getString("title");
ContentsBreakfast bfname= doc.getDocument().toObject(ContentsBreakfast.class);
breakfastList.add(bfname);
adapterb.notifyDataSetChanged();
}
}}
});
}
}
Это мой класс адаптера Recyclerview
package rimapps.appslet.lchfmalayalam.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import rimapps.appslet.lchfmalayalam.R;
import rimapps.appslet.lchfmalayalam.model.ContentsBreakfast;
import rimapps.appslet.lchfmalayalam.view.RecipeView;
public class BreakfastAdapter extends RecyclerView.Adapter<BreakfastAdapter.ViewHolder> {
List<ContentsBreakfast> breakfastlist = new ArrayList<>();
Context context;
public BreakfastAdapter(List<ContentsBreakfast> breakfastlist, Context context) {
this.breakfastlist = breakfastlist;
this.context = context;
}
@NonNull
@Override
public BreakfastAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.breakfast_card, parent, false);
return new BreakfastAdapter.ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.brfname.setText(breakfastlist.get(position).getBfname());
OkHttpClient client = new OkHttpClient();
client.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.placeholder)
.error(R.drawable.browser)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context)
.load(breakfastlist.get(position).getBfimg())
.apply(options)
.into(holder.brfimg);
holder.card_brf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(context, RecipeView.class);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return breakfastlist.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView brfname;
public ImageView brfimg;
public CardView card_brf;
public ViewHolder(View itemView) {
super(itemView);
card_brf = (CardView)itemView.findViewById(R.id.brf_card);
brfname = (TextView) itemView.findViewById(R.id.brf_name);
brfimg = (ImageView) itemView.findViewById(R.id.brf_image);
}
}}
Следующий код содержит мой класс Model
package rimapps.appslet.lchfmalayalam.model;
public class ContentsBreakfast {
int bfimg;
String bfname;
public int getBfimg() {
return bfimg;
}
public void setBfimg(int bfimg) {
this.bfimg = bfimg;
}
public String getBfname() {
return bfname;
}
public void setBfname(String bfname) {
this.bfname = bfname;
}
}