Recyclerview No Adapter Attached; Skipping layout

Asked

Viewed 3,991 times

0

My app was closing at start, I was recommended to pass all code ( creation of the Recycle view ) to the onActivityCreated from Fragment, I did this, the app works, but it should load the recycleview as soon as the app opens, however it does, do this error:

RecyclerView﹕No adapter attached; skipping layout

However when I click on the tab that opens Fragment, after the app is already open, everything works normally. I believe the problem was to have passed pro onActivityCreated, but if you put in Oncreateview, the app doesn’t even open.

Fragment:

package br.com.igoroliv.youtubecanal.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import br.com.igoroliv.youtubecanal.*;
import br.com.igoroliv.youtubecanal.API.Modelo.PlaylistItem;
import br.com.igoroliv.youtubecanal.Recycle.Adapter;


/**
 * Created by igord on 23/06/2017.
 */

public class fragmentlistavideos extends Fragment {
    private static final String TAG = "Log";
    private TextView txtteste;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private List<PlaylistItem> mylistadevideo;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragmentlistavideos, container, false);
        return v;
    }


        @Override
        public void onActivityCreated (@Nullable Bundle savedInstanceState){
            super.onActivityCreated(savedInstanceState);

            try {
                mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.mRecicleview);
                Log.d(TAG, "pegou o recycle do layout");

            } catch (Exception e) {
                Log.d(TAG, "Erro ao pegar o recycle do layout " + e);
            }

            // use this setting to improve performance if you know that changes
            // in content do not change the layout size of the RecyclerView
            mRecyclerView.setHasFixedSize(true);
            Log.d(TAG, "definiu setHasFixedSize como true");


            // defininado o LinearLayoutManager
            LinearLayoutManager llm = new LinearLayoutManager(getActivity());
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(llm);
            Log.d(TAG, "definiu LinearLayoutManager");


            mylistadevideo = ((MainActivity) getActivity()).getlistavideos();
            Log.d(TAG, "Salvou a lista no mylistadevideo");


            if (mylistadevideo == null) {
                Log.d(TAG, "Lista nula");

            } else {
                mAdapter = new Adapter(mylistadevideo);
                Log.d(TAG, "Definiu o Adapter");
            }

            // specify an adapter (see also next example)
            mRecyclerView.setAdapter(mAdapter);
            Log.d(TAG, "Setou o Adapter");


        }


}

Main Activity with click on tabs:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //click nas abas
        BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                if (tabId == R.id.tab_videos) {
                    //infla o fragment dentro do id ( contentContainer ) do framelayout
                    getSupportFragmentManager().beginTransaction()
                            .add(R.id.contentContainer, new fragmentlistavideos())
                            .replace(R.id.contentContainer,new fragmentlistavideos())
                            .commit();
                }else if (tabId == R.id.tab_chat){
                    //infla o fragment dentro do id ( contentContainer ) do framelayout
                    getSupportFragmentManager().beginTransaction()
                            .add(R.id.contentContainer, new fragmentchat())
                            .replace(R.id.contentContainer,new fragmentchat())
                            .commit();

                }
            }
        });

1 answer

0


Recyclerview calls when they are made within a context that is not the UI thread can generate this error. In my case it happened when I set the layoutmanager and everything but left without Adapter to Recycler. In your case, try to move the content creation as follows:

public class fragmentlistavideos extends Fragment {
    private static final String TAG = "Log";
    private TextView txtteste;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private List<PlaylistItem> mylistadevideo;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragmentlistavideos, container, false);

        //Se a RecyclerView estiver dentro do layout do fragment, pegue ele direto da View v inflada
        try {
            mRecyclerView = v.findViewById(R.id.mRecicleview);
            Log.d(TAG, "pegou o recycle do layout");

        } catch (Exception e) {
            Log.d(TAG, "Erro ao pegar o recycle do layout " + e);
        }

        return v;
    }

    @Override   
    public void onViewCreated(View view, Bundle savedInstanceState) {
        //Aqui você faz o preenchimento da lista.

        mRecyclerView.setHasFixedSize(true);
        Log.d(TAG, "definiu setHasFixedSize como true");

        // defininado o LinearLayoutManager
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(llm);
        Log.d(TAG, "definiu LinearLayoutManager");

        //Dica, não passe desta forma, sempre passe como parametro para o construtor
        //da fragment, ou use o pattern getInstance para fragments.
        mylistadevideo = ((MainActivity) getActivity()).getlistavideos();
        Log.d(TAG, "Salvou a lista no mylistadevideo");


        //Toda RecyclerView tem que obrigatóriamente ter um adapter, mesmo que seja com uma 
        //lista vazia, nunca null
        if (mylistadevideo == null) {           
            mRecyclerView.setAdapter(new ArrayList<PlaylistItem>());
            Log.d(TAG, "Setou o Adapter");
            Log.d(TAG, "Lista nula");
        } else {
            mAdapter = new Adapter(mylistadevideo);
            Log.d(TAG, "Definiu o Adapter");
        }
    }

    @Override
    public void onActivityCreated (@Nullable Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
    }
}
  • From what I understand you recommend calling the api and using the list with the result inside the right ? where? can be inside the Onviewcreate?

  • I managed, thank you, I passed all the call inside the onviewcreate, and put to set the Adapter inside the retrofit Sponse, everything working :)

  • I just couldn’t create an empty list to put in if the list returns null.

  • if it returns null, instance it empty (new Arraylist<Playlistitem>())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.