当我使用activity
,fragment
和recyclerview
启动adapter
时,该应用正在停止。我不知道问题出在哪里。我尝试了不同的方法。
我的logcat:
java.lang.NullPointerException:尝试调用虚方法'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView $适配器)” 在null对象引用上 在com.orthodox.FeedActivity.onStart(FeedActivity.java:133) 在android.support.v4.app.Fragment.performStart(Fragment.java:2287)
我的代码:
public class FeedActivity extends Fragment{
private RecyclerView mBlogList;
private Query mQuery;
private Query mQueryck;
private DatabaseReference mDatabaseUsers;
private FirebaseUser mCurrent;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
FirebaseRecyclerAdapter mAdapter;
public FeedActivity() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final FragmentActivity c = getActivity();
View layout = inflater.inflate(R.layout.fragment_feed, container, false);
return layout;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mBlogList = (RecyclerView) view.findViewById(R.id.blog_list);
mBlogList.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mBlogList.setLayoutManager(layoutManager);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() == null){
Intent loginIntent = new Intent(getActivity(), SignUpActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}
}
};
mQuery = FirebaseDatabase.getInstance().getReference().child("Blog").limitToLast(20);
mQuery.keepSynced(true);
mQueryck = FirebaseDatabase.getInstance().getReference().child("Block").limitToLast(20);
mQueryck.keepSynced(true);
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("users");
mDatabaseUsers.keepSynced(true);
mCurrent = mAuth.getCurrentUser();
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>().setQuery(mQuery, User.class).build();
FirebaseRecyclerOptions<User> optionss = new FirebaseRecyclerOptions.Builder<User>().setQuery(mQueryck, User.class).build();
mAdapter = new FirebaseRecyclerAdapter<User, BlogViewHolder>(options) {
@Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_row, parent, false);
return new BlogViewHolder(view);
}
@Override
protected void onBindViewHolder(BlogViewHolder viewHolder, int position, User model) {
final String post_key = getRef(position).getKey();
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setTime(model.getFirstname());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
mBlogList.setAdapter(mAdapter);
mAdapter.startListening();
mAdapter = new FirebaseRecyclerAdapter<User, BlogViewHolder>(optionss) {
@Override
protected void onBindViewHolder(BlogViewHolder viewHolder, int position, User model) {
final String post_key = getRef(position).getKey();
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setTime(model.getFirstname());
viewHolder.setImage(getApplicationContext(), model.getImage());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent bsingle = new Intent(getActivity(), SinglePostActivity.class);
bsingle.putExtra("post", post_key);
startActivity(bsingle);
}
});
}
@Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_row, parent, false);
return new BlogViewHolder(view);
}
};
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc){
TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImage(Context ctx, String image){
if (image != null) {
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
else {
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
post_image.setVisibility(View.GONE);
}
}
public void setTime(String time){
TextView post_time = (TextView) mView.findViewById(R.id.post_username);
post_time.setText(time);
}
答案 0 :(得分:0)
在onCreateView
方法中初始化所有观看次数。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final FragmentActivity c = getActivity();
View layout = inflater.inflate(R.layout.fragment_feed, container, false);
mBlogList = (RecyclerView) layout.findViewById(R.id.blog_list);
return layout;
}