recyclerview No adapter attached; skipping layout

后端 未结 30 2935
走了就别回头了
走了就别回头了 2020-11-21 04:51

Just implemented RecyclerView in my code, replacing ListView.

Everything works fine. The data is displayed.

But error messages are

相关标签:
30条回答
  • 2020-11-21 05:22

    Adding yet another answer since I came across this thread googling the error. I was trying to initialize a PreferenceFragmentCompat but I forgot to inflate the preference XML in onCreatePreferences like this:

    class SettingsFragment : PreferenceFragmentCompat() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            val inflater = LayoutInflater.from(context)
            inflater.inflate(R.layout.fragment_settings, null)
        }
    
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            // Missing this line here:
            //   setPreferencesFromResource(R.xml.settings, rootKey)
        }
    }
    

    The error was a mystery until I realized that PreferenceFragmentCompat must be using a RecyclerView internally.

    0 讨论(0)
  • 2020-11-21 05:23

    i have this problem , a few time problem is recycleView put in ScrollView object

    After checking implementation, the reason appears to be the following. If RecyclerView gets put into a ScrollView, then during measure step its height is unspecified (because ScrollView allows any height) and, as a result, gets equal to minimum height (as per implementation) which is apparently zero.

    You have couple of options for fixing this:

    1. Set a certain height to RecyclerView
    2. Set ScrollView.fillViewport to true
    3. Or keep RecyclerView outside of ScrollView. In my opinion, this is the best option by far. If RecyclerView height is not limited - which is the case when it's put into ScrollView - then all Adapter's views have enough place vertically and get created all at once. There is no view recycling anymore which kinda breaks the purpose of RecyclerView .

    (Can be followed for android.support.v4.widget.NestedScrollView as well)

    0 讨论(0)
  • 2020-11-21 05:23

    I have solved this error. You just need to add layout manager and add the empty adapter.

    Like this code:

    myRecyclerView.setLayoutManager(...//your layout manager);
            myRecyclerView.setAdapter(new RecyclerView.Adapter() {
                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    return null;
                }
    
                @Override
                public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    
                }
    
                @Override
                public int getItemCount() {
                    return 0;
                }
            });
    //other code's 
    // and for change you can use if(mrecyclerview.getadapter != speacialadapter){
    //replice your adapter
    //}
    
    0 讨论(0)
  • 2020-11-21 05:24

    In your RecyclerView adapter class, for example MyRecyclerViewAdapter, make a constructor with the following params.

    MyRecyclerViewAdapter(Context context, List<String> data) {
        this.mInflater = LayoutInflater.from(context); // <-- This is the line most people include me miss
        this.mData = data;
    }
    

    mData is the data that you'll pass to the adapter. It is optional if you have no data to be passed. mInflater is the LayoutInflater object that you have created and you use in the OnCreateViewHolder function of the adapter.

    After this, you attach the adapter in the MainActivity or wherever you want to on the main/UI thread properly like

    MyRecyclerViewAdapter recyclerAdapter;
    OurDataStuff mData;
    
        ....
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Like this:
    
            RecyclerView recyclerView = findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerAdapter = new RecyclerAdapter(this, mData); //this, is the context. mData is the data you want to pass if you have any
            recyclerView.setAdapter(recyclerAdapter);
        }
       ....
    
    0 讨论(0)
  • 2020-11-21 05:26

    In my situation it was a forgotten component which locates in ViewHolder class but it wasn't located in layout file

    0 讨论(0)
  • 2020-11-21 05:29

    Can you make sure that you are calling these statements from the "main" thread outside of a delayed asynchronous callback (for example inside the onCreate() method). As soon as I call the same statements from a "delayed" method. In my case a ResultCallback, I get the same message.

    In my Fragment, calling the code below from inside a ResultCallback method produces the same message. After moving the code to the onConnected() method within my app, the message was gone...

    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    list.setLayoutManager(llm);
    list.setAdapter( adapter );
    
    0 讨论(0)
提交回复
热议问题