无法使用FirebaseRecyclerViewAdapter从Firebase实时数据库检索数据

时间:2018-12-28 11:09:20

标签: android firebase firebase-realtime-database recycler-adapter firebaseui

无法从数据库检索Post键和值。这是我第一次尝试使用FirebaseRecyclerAdapter从Firebase数据库检索数据。但是我不知道问题出在哪里。我也尝试调试,但是编译器不会进入onCreateViewHolder或onBindViewHolder函数。但是我完全遵循FirebaseUI进行实时数据库参考。

NEWSFEEDBANK.JAVA

private FirebaseRecyclerAdapter<NewsFeed, NewsFeedViewHolder> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news_feed_bank);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    SharedPreferences sharedPreferences = getSharedPreferences("BankData", Context.MODE_PRIVATE);
    username = sharedPreferences.getString("UserName", "UserName");

    recyclerView = (RecyclerView) findViewById(R.id.recycler_View_Bank);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
    mAuth = FirebaseAuth.getInstance();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (mAuth.getCurrentUser() == null) {
                Intent loginIntent = new Intent(NewsFeedBank.this, Login.class);
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(loginIntent);
            }
        }
    };

    DisplayPosts();
}

private void DisplayPosts() {

    FirebaseRecyclerOptions<NewsFeed> options = new FirebaseRecyclerOptions.Builder<NewsFeed>().setQuery(mDatabase, NewsFeed.class).build();

    adapter = new FirebaseRecyclerAdapter<NewsFeed, NewsFeedViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull NewsFeedViewHolder holder, int position, @NonNull NewsFeed model) {

          //  holder.title.setText(model.getTitle());
        //    holder.des.setText(model.getDesc());
            holder.usern.setText(model.getUsername());

        }

        @NonNull
        @Override
        public NewsFeedViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_items, viewGroup, false);
            NewsFeedViewHolder holder = new NewsFeedViewHolder(view);
            return holder;
        }
    };
    recyclerView.setAdapter(adapter);


}

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}

public static class NewsFeedViewHolder extends RecyclerView.ViewHolder {
    TextView title, des, usern;
    View mView;

    public NewsFeedViewHolder(@NonNull View itemView) {
        super(itemView);
        usern = (TextView) mView.findViewById(R.id.view_poster_username);
    }

}}

NEWSFEED.JAVA

导入com.google.firebase.storage.StorageReference;

公共类NewsFeed {

private String Title, Description ,UserName;
private StorageReference imageUrl;

public NewsFeed(String UserName, String Title, String Description) {
    this.Title = Title;
    this.Description = Description;
    this.UserName = UserName;
}
public NewsFeed() {
}

public void setUsername(String UserName) {
    this.UserName = UserName;
}
public void setTitle(String Title) {
    this.Title = Title;
}
public void setDesc(String Description) {
    this.Description = Description;
}

public String getTitle() {
    return Title;
}
public String getDesc() {
    return Description;
}
public String getUsername() {
    return UserName;
}}
  

E / Android运行时:致命异常:主要       流程:com.example.admin.firstapp,PID:1151       java.lang.NullPointerException:尝试在null上调用虚拟方法'android.view.View android.view.View.findViewById(int)'   对象参考           在com.example.admin.firstapp.NewsFeedBank $ NewsFeedViewHolder。(NewsFeedBank.java:119)           at com.example.admin.firstapp.NewsFeedBank $ 3.onCreateViewHolder(NewsFeedBank.java:92)           在com.example.admin.firstapp.NewsFeedBank $ 3.onCreateViewHolder(NewsFeedBank.java:78)           在android.support.v7.widget.RecyclerView $ Adapter.createViewHolder(RecyclerView.java:6794)           在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5858)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5854)           在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2230)           在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)           在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)           在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)           在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)           在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)           在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)           在android.view.View.layout(View.java:20672)           在android.view.ViewGroup.layout(ViewGroup.java:6194)           在android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1183)           在android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:870)           在android.support.design.widget.ViewOffsetBehavior.layoutChild(ViewOffsetBehavior.java:62)           在android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:146)           在android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)           在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)           在android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)

Firebase数据库结构: Firebase Database Structure

1 个答案:

答案 0 :(得分:1)

尝试

usern = (TextView) itemView.findViewById(R.id.view_poster_username);

代替

usern = (TextView) mView.findViewById(R.id.view_poster_username);

如果再次遇到错误,请发布您的布局文件