RecyclerView: No adapter attached; skipping layout

0
My app was closing at startup, I was advised to pass all code (recycle view child) to the onActivityCreated of the fragment, I did this, the app works, but it should load recycleview as soon as the app opens, but it does not , gives this error:

RecyclerView﹕No adapter attached; skipping layout

But when I click on the tab that opens the fragment, after the app is already open, everything works normally. I believe the problem was to have passed on to the onActivityCreated, but if you put it in OncreateView, the app does not 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 the 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();

                }
            }
        });
    
asked by anonymous 25.06.2017 / 04:19

1 answer

0

The calls from recyclerview when they are made within a context that are not from the UI thread may generate this error. In my case it happened when I moved the layoutmanager and everything else but left without adapter to recycler. In your case, try moving 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);
    }
}
    
26.06.2017 / 13:24