【问题标题】:Error: E/RecyclerView: No adapter attached; skipping layout on Fragments错误:E/RecyclerView:未连接适配器;跳过片段上的布局
【发布时间】:2021-05-28 03:11:27
【问题描述】:

我正在尝试使用 FireBase 数据库在片段上显示 RecyclerView,但由于某种未知原因,它给了我这个错误:E/RecyclerView: No adapter attach;我很确定我实际上连接了适配器,这是代码:

主要活动

public class MainActivity extends AppCompatActivity {

    Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState == null){
            currentFragment = new MapsFragment();
            changeFragment(currentFragment);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_bookmarkList:
                currentFragment = new ListaFragment();
                break;
            case R.id.menu_mapa:
                currentFragment = new MapsFragment();
                break;

        }
        changeFragment(currentFragment);
        return super.onOptionsItemSelected(item);

    }

    private void changeFragment(Fragment currentFragment) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,currentFragment).commit();
    }
    
}

ListaFragment(应该显示 Recycler 的那个):

public class ListaFragment extends Fragment implements MyAdapter.RecyclerItemClick{


    private MyAdapter myAdapter;
    private RecyclerView recycler;
    FirebaseDatabase database;
    DatabaseReference myRef;

    public ListaFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_lista, container, false);

        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("Marcador");
        
        recycler = (RecyclerView) v.findViewById(R.id.recyler);

        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        
        FirebaseRecyclerOptions<Marcador> options = new FirebaseRecyclerOptions.Builder<Marcador>()
                .setQuery(myRef, Marcador.class).build();
        myAdapter = new MyAdapter(options,this);

        recycler.setAdapter(myAdapter);

        return v;

    }
    
    @Override
    public void onStart(){
        super.onStart();
        myAdapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        myAdapter.stopListening();

    }

    @Override
    public void itemClick(Marcador marcador) {

    }
}

我已经完成了适配器(我跳过了 xml,因为它们非常简单):

public class MyAdapter extends FirebaseRecyclerAdapter<Marcador,MyAdapter.MarcadorHolder> {

    private Context context;
    private RecyclerItemClick itemClick;

    public MyAdapter(@NonNull FirebaseRecyclerOptions<Marcador> options, RecyclerItemClick itemClick) {
        super(options);
        this.itemClick = itemClick;
    }

    @Override
    protected void onBindViewHolder(@NonNull final MarcadorHolder holder, int position, @NonNull Marcador model) {
        final Marcador marcador = getItem(position);
        holder.textViewNom.setText(model.getNom());
        holder.textViewLatitude.setText(String.valueOf(model.getLatitude()));
        holder.textViewLongitude.setText(model.getLongitude());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClick.itemClick(marcador);
            }
        });
    }

    @NonNull
    @Override
    public MarcadorHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recylcer_view_item,parent,false);
        context = parent.getContext();
        return new MarcadorHolder(v);
    }

    public class MarcadorHolder extends RecyclerView.ViewHolder{
        TextView textViewNom;
        TextView textViewLatitude;
        TextView textViewLongitude;

        public MarcadorHolder(@NonNull View itemView) {
            super(itemView);

            textViewNom = itemView.findViewById(R.id.textViewNom);
            textViewLatitude = itemView.findViewById(R.id.textViewLatitud);
            textViewLongitude = itemView.findViewById(R.id.textViewLongitut);
        }

    }

    public interface RecyclerItemClick {
        void itemClick(Marcador marcador);
    }
}

我希望有人能回答我为什么会发生这种情况,因为我已经检查了其他选项,但我无法真正通过它。

XML 的附加片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.ListaFragment">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyler"
        android:layout_width="match_parent"
        android:layout_height="545dp" />

</LinearLayout>

回收站视图项目的XML附加:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="60dp" android:background="#CDCDCD" android:layout_marginBottom="5dp"
    >


    <TextView
        android:id="@+id/textViewNom"
        android:layout_width="410dp"
        android:layout_height="55dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="5dp"
        android:text="Title"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textViewLatitud"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="35dp"
        android:text="latitud" />

    <TextView
        android:id="@+id/textViewLongitut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="125dp"
        android:layout_marginTop="35dp"
        android:text="longitut" />


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_marginLeft="265dp"
        android:layout_marginTop="10dp"
        />


</RelativeLayout>

【问题讨论】:

    标签: android android-fragments firebase-realtime-database android-recyclerview firebaseui


    【解决方案1】:

    请使用下面的行

    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recycler.setLayoutManager(layoutManager);
    recycler.setAdapter(adapter);
        
    

    【讨论】:

    • 你能更清楚地解释你的答案吗?喜欢使用此代码与 OP 的尝试有何不同?
    • 我们总是需要 recyclerview 的布局管理器,因为您已在代码中添加,但您尚未在代码中添加方向。尝试使用上面的代码行,看看这些是否适合你。
    【解决方案2】:

    根据Fragment API Reference

    建议只在此方法中对布局进行膨胀,并将对返回的 View 进行操作的逻辑移动到 onViewCreated(View, Bundle) 中。

    所以在onCreateView 中,您应该只扩展您的布局,因此您使用的View v = inflater.inflate(R.layout.fragment_lista, container, false); 并将您的代码逻辑移动到onViewCreated,而您面临E/RecyclerView: No adapter attached; 的原因是因为您正在尝试为您的recyclerview 在创建/扩展视图之前无法完成,因此在创建/扩展完成后没有适配器连接到 recyclerview。

    【讨论】:

      【解决方案3】:

      一般建议不要在onViewCreated()中使用findViewById初始化组件,按照The Google Developer Documents.

      你应该在 onCreateView 中增加你的布局,但不应该 使用 onCreateView 中的 findViewById 初始化其他视图。

      因为在此方法中,并非每个视图都未正确初始化。因此,最好使用onViewCreated() 来获取视图的 ID 并设置 UI。

      【讨论】:

      • 您能否指出文档中的确切位置:“您应该在 onCreateView 中扩展您的布局,但不应该在 onCreateView 中使用 findViewById 初始化其他视图。” ?
      • 我认为我的链接已经指出了同样的事情。
      • 我什至尝试过使用 onViewCreated() 可惜没有运气:/
      • 那你能把xml文件也附上吗?
      • @animusmind 我现在在帖子的消息中添加了 XML(片段+项目),也许我错过了什么
      猜你喜欢
      • 2020-03-12
      • 1970-01-01
      • 2021-12-07
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      相关资源
      最近更新 更多