Не удалось получить ключи и значения 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
import 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 / AndroidRuntime: FATAL EXCEPTION: main Процесс: com.example.admin.firstapp, PID: 1151 java.lang.NullPointerException: попытка вызвать виртуальный метод 'android.view.View android.view.View.findViewById (int)' для пустой ссылки на объект в com.example.admin.firstapp.NewsFeedBank $ NewsFeedViewHolder. (NewsFeedBank.java:119) в com.example.admin.firstapp.NewsFeedBank $ 3.onCreateViewHolder (NewsFeedBank.java:92) в com.example.admin.firstapp.NewsFeedBank $ 3.onCreateViewHolank (NewsroidjedB).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 $.следующий (LinearLayoutManager.java:2230) в android.support.v7.widget.LinearLayoutManager.layoutChunk (LinearLayoutManager.java:1557) в android.support.v7.widget.LinearLayoutManager.fill (LinearLayoutroid1517pp atj.v7.widget.LinearLayoutManager.onLayoutChildren (LinearLayoutManager.java:612) в android.support.v7.widget.RecyclerView.dispatchLayoutStep2 (RecyclerView.java:3924) в android.support.vcycler.Rej (Reader.jp): 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.CoordinatorLayoutildonon(CoordinatorLayout.java:870) в android.support.design.widget.ViewOffsetBehavior.layoutChild (ViewOffsetBehavior.java:62) в android.support.design.widget.HeaderScrollingViewBehavior.layoutChild (HeaderSc6). HeaderSc6ДизайнJava: 888)
Структура базы данных Firebase: