错误消息:E/RecyclerView:未连接适配器;跳过布局

Posted

技术标签:

【中文标题】错误消息:E/RecyclerView:未连接适配器;跳过布局【英文标题】:error message: E/RecyclerView: No adapter attached; skipping layout 【发布时间】:2021-12-07 05:12:44 【问题描述】:

一切正常,但数据不显示。

但是当我签入“logcat”时,我发现了这个错误消息:

2021-10-20 11:29:37.387 7327-7327/com.example.myplay E/RecyclerView:未连接适配器;跳过布局

2021-10-20 11:29:37.388 7327-7327/com.example.myplay E/RecyclerView:未连接适配器;跳过布局

MainActivity.java:

public class MainActivity extends AppCompatActivity 
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference();
    private List<DataModel> dataModels;
    private  SliderAdapter sliderAdapter;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Tech Videos");

        FirebaseApp.initializeApp(this);

        SliderView sliderView = findViewById(R.id.sliderView);

        sliderAdapter  = new SliderAdapter(this);
        sliderView.setSliderAdapter(sliderAdapter);
        sliderView.setIndicatorAnimation(IndicatorAnimationType.WORM);
        sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
        sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
        sliderView.setIndicatorSelectedColor(Color.WHITE);
        sliderView.setIndicatorUnselectedColor(Color.GRAY);
        sliderView.setScrollTimeInSec(6);   // 6 sec is slider
        sliderView.setAutoCycle(true);
        sliderView.startAutoCycle();
        renewItems(sliderView);

        loadFirebaseForSlider();
    

    private void loadFirebaseForSlider() 
        myRef.child("trailer").addListenerForSingleValueEvent(new ValueEventListener() 
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) 
                for(DataSnapshot contentSlider: snapshot.getChildren())

                    DataModel sliderItem = contentSlider.getValue(DataModel.class);
                    dataModels.add(sliderItem);
                
                sliderAdapter.notifyDataSetChanged();

            

            @Override
            public void onCancelled(@NonNull DatabaseError error) 
                Toast.makeText(MainActivity.this, "Data can't fetch"+error.getMessage(), Toast.LENGTH_SHORT).show();
                finish();
            
        );
    

    public void loadSliderAdapter()
        sliderAdapter.notifyDataSetChanged();
    

    public void renewItems(View view) 
        dataModels = new ArrayList<>();
        DataModel dataItems = new DataModel();
        dataModels.add(dataItems);
        sliderAdapter.renewItems(dataModels);
        sliderAdapter.deleteItems(0);
    

DataModel.java:

package com.example.myplay;

public class DataModel 

    private String Ttitle;
    private String Turl;
    private String Tvid;

    public DataModel() 
    

    public DataModel(String ttitle, String turl, String tvid)
        Ttitle = ttitle;
        Turl = turl;
        Tvid = tvid;
    

    public String getTtitle() 
        return Ttitle;
    

    public void setTtitle(String ttitle) 
        Ttitle = ttitle;
    

    public String getTurl() 
        return Turl;
    

    public void setTurl(String turl) 
        Turl = turl;
    

    public String getTvid() 
        return Tvid;
    

    public void setTvid(String tvid) 
        Tvid = tvid;
    

SliderAdapter.java

package com.example.myplay;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import com.smarteist.autoimageslider.SliderViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class SliderAdapter extends SliderViewAdapter<SliderAdapter.MyViewHolder> 

     private Context context;


    public SliderAdapter(Context context) 
        this.context = context;
    

    private List<DataModel> dataModels = new ArrayList<>();

    public void renewItems(List<DataModel> dataModels)

        this.dataModels = dataModels;
        notifyDataSetChanged();
    
    public void deleteItems(int position)
        this.dataModels.remove(position);
        notifyDataSetChanged();
    

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent) 

      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.slider_item,
              parent,false);
        return new MyViewHolder(view);


    

    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, int position) 

        viewHolder.trailer_title.setText(dataModels.get(position).getTtitle());
        Glide.with(viewHolder.itemView).load(dataModels.get(position).getTurl()).into(viewHolder.slider_image);
        viewHolder.play_button.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Intent trailer_video = new Intent(context,PlayerActivity.class);
                trailer_video.putExtra("vid",dataModels.get(position).getTvid());
                trailer_video.putExtra("title",dataModels.get(position).getTtitle());
                v.getContext().startActivity(trailer_video);
            
        );


    

    @Override
    public int getCount() 
        return dataModels.size();
    


     public  class MyViewHolder extends SliderViewAdapter.ViewHolder
        ImageView slider_image;
        TextView trailer_title;
        FloatingActionButton play_button;


        public MyViewHolder(View itemView) 
            super(itemView);

            slider_image = itemView.findViewById(R.id.image_thumbnail);
            trailer_title = itemView.findViewById(R.id.trailer_title);
            play_button = itemView.findViewById(R.id.floatingActionButton);

        
    

【问题讨论】:

您在代码中的哪个位置设置了适配器?这个answer 很可能会有所帮助。 我创建的名为“slideAdapter”的适配器 【参考方案1】:

正如 Alex 所说,您还没有告诉视图使用您创建的适配器,这意味着视图对适配器及其数据一无所知。

要解决此问题,请在 onCreate 中初始化它们之后将适配器设置为视图:

...
SliderView sliderView = findViewById(R.id.sliderView);

sliderAdapter  = new SliderAdapter(this);
...

sliderView.setAdapter(sliderAdapter); // ?

【讨论】:

我上传了更多代码,你可以再检查一下吗 据我所知,您仍然没有在任何地方打电话给setAdapter。您是否尝试将我在答案中标记的行添加到您自己的代码中? 我试过了但是没用sliderView.setAdapter(sliderAdapter);错误行“setAdapter”,在 Google 上我发现 RecyclerView 中的这个 setAdapter (androidx.recyclerview.widget.RecyclerView.Adapter) 不能应用于 (example.example.com.project.ProductAdapter)

以上是关于错误消息:E/RecyclerView:未连接适配器;跳过布局的主要内容,如果未能解决你的问题,请参考以下文章

错误 ANDROID STUDIO E/RecyclerView:未连接适配器;跳过布局

尝试设置适配器:不断获取 E/RecyclerView:未连接适配器;跳过布局[重复]

Recyclerview 未连接适配器

E/RecyclerView:没有附加适配器;在片段内的recyclerview上跳过布局

E/RecyclerView:没有附加适配器;跳过布局。尽管初始化适配器

Fragment,RecyclerView:没有附加适配器;跳过布局