我在mRecyclerView.setLayoutManager(layoutManager);
以下是在进入活动时弹出的logcat错误:
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)” 在com.example.lenovo.skanda.QuoteList.onCreate(QuoteList.java:36)
QuoteList.java
public class QuoteList extends AppCompatActivity {
private StorageReference storageRef;
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote_list);
//ActionBar actionBar = getSupportActionBar();
//getSupportActionBar().setTitle("Quotes");
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//getSupportActionBar().setHomeButtonEnabled(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView = findViewById(R.id.recyclerViewQuote);
// mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mReference = mFirebaseDatabase.getReference("Quotes");
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Quote, QuoteViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Quote, QuoteViewHolder>(
Quote.class,
R.layout.quote_row,
QuoteViewHolder.class,
mReference
) {
@Override
protected void populateViewHolder(QuoteViewHolder viewHolder , Quote model , int position) {
// viewHolder.setDetails(getApplicationContext(), model.getQuotes(), model.getQuote_Author(), model.getImages());
viewHolder.setQuotes(model.getQuotes());
viewHolder.setQuoteAuthor(model.getQuote_Author());
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class QuoteViewHolder extends RecyclerView.ViewHolder{
View mView;
public QuoteViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setQuotes(String Quote){
TextView post_quote = (TextView) mView.findViewById(R.id.post_quote);
post_quote.setText(Quote);
}
public void setQuoteAuthor(String QuoteAuthor){
TextView post_quoteAuthor = (TextView) mView.findViewById(R.id.post_author);
post_quoteAuthor.setText(QuoteAuthor);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
请帮帮我。
答案 0 :(得分:0)
您的RecyclerView
尚未分配,请交换以下内容:
mRecyclerView = findViewById(R.id.recyclerViewQuote);
mRecyclerView.setLayoutManager(layoutManager);
答案 1 :(得分:0)
请编写这样的代码,您首先需要在Linear Layout Manager之前初始化recyclerview。
#include <iostream>
#include "Point.h"
#include "Line.h"
using namespace std;
int main() {
int x, y;
int size; // Some user defined size for the array
Line line;
Point a[size]; // Some points that are already filled
// Push the data in a[] to the variable "line"
for(int i = 0; i < size; i++){
// Increase array size of Point* points in variable line and add a[i] to the end of the array
line.push(points[i]);
}
return 0;
}
答案 2 :(得分:0)
当LinearLayout
在布局中找不到RecyclerView
时,就会发生这种情况。
您必须在要显示列表的布局文件中添加RecyclerView
,
否则,在使用RecyclerView
或未使用findViewById(R.id.recyclerviewID)
时会错误地提及创建的RecyclerView
。