我正在使用FirebaseRecyclerAdapter开发Firebase Blogging应用程序。主要问题是由于onStart()中的setAdapter引起的,由于此问题的发生。在运行该活动时,我的日志记录会弹出以下错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
at com.firebase.ui.database.FirebaseArray.onCreate(FirebaseArray.java:54)
at com.firebase.ui.common.BaseObservableSnapshotArray.addChangeEventListener(BaseObservableSnapshotArray.java:97)
at com.firebase.ui.database.FirebaseRecyclerAdapter.startListening(FirebaseRecyclerAdapter.java:49)
at com.example.lenovo.skanda.QuoteList.onStart(QuoteList.java:58)
我在setAdapter上遇到错误。
QuoteList.java
public class QuoteList extends AppCompatActivity {
private RecyclerView mQuotesList;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote_list);
mQuotesList = (RecyclerView) findViewById(R.id.quotes_list);
mQuotesList.setHasFixedSize(true);
mQuotesList.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Quote> options =
new FirebaseRecyclerOptions.Builder<Quote>()
.setQuery(mDatabase, Quote.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Quote, QuoteViewHolder>(options) {
@Override
public QuoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.quote_row, parent, false);
return new QuoteViewHolder(view);
}
@Override
protected void onBindViewHolder(QuoteViewHolder holder, int position, Quote model) {
holder.setQuote(model.getQuotes());
holder.setAuthor(model.getQuote_Author());
}
};
mDatabase.setAdapter(adapter);
}
public static class QuoteViewHolder extends RecyclerView.ViewHolder{
View mView;
public QuoteViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setQuote(String Quote){
TextView post_quote = (TextView) mView.findViewById(R.id.post_quote);
post_quote.setText(Quote);
}
public void setAuthor(String Author){
TextView post_author = (TextView) mView.findViewById(R.id.post_author);
post_author.setText(Author);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
Quote.java
package com.example.lenovo.skanda;
public class Quote {
private String Images, Quote_Author, Quotes;
public Quote(){
}
public Quote(String images, String quote_Author, String quotes) {
Images = images;
Quote_Author = quote_Author;
Quotes = quotes;
}
public String getImages() {
return Images;
}
public void setImages(String images) {
Images = images;
}
public String getQuote_Author() {
return Quote_Author;
}
public void setQuote_Author(String quote_Author) {
Quote_Author = quote_Author;
}
public String getQuotes() {
return Quotes;
}
public void setQuotes(String quotes) {
Quotes = quotes;
}
}
在代码中获取错误:`mDatabase.setAdapter(adapter);
无法解析方法'setAdapter(com.firebase.ui.database.FirebaseRecyclerAdapter)'`
请帮助我。.非常感谢您。