0

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
NuiDev
  • 131
  • 1
  • 9

1 Answers1

0

The stacktrace suggests that Your txt_comic TextView is null at the time of calling setText on it. It is caused by the txt_comic not being initialized while the method onComicLoadDoneListener is called after the call to loadComic.

Simply swap the order of txt_comic initialization and the loadComic call:

// Init List things
recycler_comic = (RecyclerView)rootView.findViewById(R.id.RV_Discover);
txt_comic = (TextView)rootView.findViewById(R.id.comic_title);

loadComic();

Second problem is probably the fact, that You are not attaching an Adapter immediately after initializing the RecyclerView. A workaround in Your case would be to pass a null or MyComicAdapter with an empty list:

recycler_comic = (RecyclerView)rootView.findViewById(R.id.RV_Discover);
recycler_comic.setAdapter(new MyComicAdapter(getActivity(), new ArrayList<>())); // or recycler_comic.setAdapter(null);

This should resolve Your problem.

sweak
  • 1,369
  • 2
  • 6
  • 21