I was following a tutorial about a comic reading app, and while almost everything works, the last part of my code does not work, it says that my RecylcerView has no adapter attached to it, but it does, and because of that my textview cant be set resulting in a app crash
Here is the fragment.xml where the error happens!
public class fragment_discover extends Fragment implements I_ComicLoadDone {
// Database
DatabaseReference comics;
I_ComicLoadDone comicListener;
RecyclerView recycler_comic;
TextView txt_comic;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_discover,container,false);
// Init Database
comics = FirebaseDatabase.getInstance().getReference("Comic");
// Init Listener
comicListener = this;
loadComic();
// Init List things
recycler_comic = (RecyclerView)rootView.findViewById(R.id.RV_Discover);
txt_comic = (TextView)rootView.findViewById(R.id.comic_title);
return rootView;
}
private void loadComic() {
comics.addListenerForSingleValueEvent(new ValueEventListener() {
List<Comic> comic_load = new ArrayList<>();
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot comicSnapShot: dataSnapshot.getChildren())
{
Comic comic = comicSnapShot.getValue(Comic.class);
comic_load.add(comic);
}
comicListener.onComicLoadDoneListener(comic_load);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onComicLoadDoneListener(List<Comic> comicList) {
Common.comicList = comicList;
recycler_comic.setAdapter(new MyComicAdapter(getActivity(), comicList));
txt_comic.setText(new StringBuilder("NEW COMIC (")
.append(comicList.size())
.append(")"));
}
}
I didnt put the adapter code in here, because that one works fine, if you need it just ask.
After launching the app I get this error message:
E/RecyclerView: No adapter attached; skipping layout
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference