简体   繁体   English

RecyclerView:未连接适配器-为什么我不断收到此错误?

[英]RecyclerView: No adapter attached - why do I keep getting this error?

Just trying to show in a RecyclerView the info from my database. 只是尝试在RecyclerView中显示我数据库中的信息。 everything works fine. 一切正常。 The objects are displayed. 显示对象。

but logcat says: 但是logcat说:

E/RecyclerView: No adapter attached; skipping layout

This is my the code (DisplayImages.java) 这是我的代码(DisplayImages.java)

    public class DisplayImages extends AppCompatActivity {
    DatabaseReference databaseReference;

    RecyclerView recyclerView;

    RecyclerView.Adapter adapter;
    ProgressDialog progressDialog;

    List<ImageUploadInfo> list = new ArrayList<>();

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

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(DisplayImages.this));

        progressDialog = new ProgressDialog(DisplayImages.this);
        progressDialog.setMessage("Loading Images From Firebase.");
        progressDialog.show();

        // Setting up Firebase image upload folder path in databaseReference.
        // The path is already defined in MainActivity.
        databaseReference = FirebaseDatabase.getInstance().getReference(Main.Database_Path);

        databaseReference.addValueEventListener(new ValueEventListener() {
            @override
            public void onDataChange(DataSnapshot snapshot) {

                for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                    ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);

                    list.add(imageUploadInfo);
                }

                adapter = new RecyclerViewAdapter(getApplicationContext(), list);

                recyclerView.setAdapter(adapter);
                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

                progressDialog.dismiss();

            }
        });

    }

And If it makes any difference, this is what i wrote in MainActivity about the DataBase_Path : 如果有什么不同,这就是我在MainActivity中写的有关DataBase_Path的内容:

public class Main extends AppCompatActivity implements View.OnClickListener {

DatabaseReference databaseReference;
public static final String Database_Path = "All_Image_Uploads_Database";

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

    databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);

As you can see I have attached an adapter for Recycleview. 如您所见,我已为Recycleview连接了适配器。 so why do I keep getting this error? 那为什么我总是收到这个错误?

i have read other questions related to same problem but none helps. 我已经阅读了与同一问题相关的其他问题,但没有帮助。

I don't see anywhere you are creating an adapter and setting that adapter to RecyclerView . 我看不到要创建适配器并将该适配器设置为RecyclerView任何地方。 You need to create an adapter and set adapter like following: 您需要创建一个适配器并设置适配器,如下所示:

mAppAdapter = new AppAdapter(mModel); // or whatever constructor you want
recyclerView.setAdapter(mAppAdapter);

EDIT: 编辑:

You are setting adapter to RecyclerView inside an asynchronous method. 您正在异步方法RecyclerView适配器设置为RecyclerView So your program will have a non deterministinc amount of time where the adapter is not actually set to recyclerView . 因此,您的程序实际上没有确定适配器设置为recyclerView To get rid off the warning you get you should initialize and set the adapter above the async method and only update data of the adapter inside the async method 要摆脱警告,您应该初始化适配器并将其设置在async方法上方,并且仅更新async方法内部的适配器数据

Yyou need to set the adapter after you set the layour manager, otherwise you'll get the error message you're seeing. 您需要在设置Layour Manager 之后设置适配器,否则将收到错误消息。

recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
//don't set adapter here
    recyclerView.setLayoutManager(new LinearLayoutManager(DisplayImages.this));

//set adapter here

Moreover, you can set the adapter with an empty list first and then when you receive the callback from firebase you can update the list and call notifyDatasetChanged() on the adapter. 此外,您可以先将适配器设置为空列表,然后从firebase收到回调时,可以更新列表并在适配器上调用notifyDatasetChanged()。 The problem you see is due to the fact that you're not actually setting the adapter until very later in the process (when your firebase call comes back) 您看到的问题是由于您直到该过程的稍后阶段才真正设置适配器(当您的Firebase调用返回时)

Like this: 像这样:

recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(DisplayImages.this));
adapter = new RecyclerViewAdapter(getApplicationContext(), list);
 recyclerView.setAdapter(adapter);


databaseReference.addValueEventListener(new ValueEventListener() {
            @override
            public void onDataChange(DataSnapshot snapshot) {

                for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                    ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);

                    list.add(imageUploadInfo);
                }
               adapter.notifyDatasetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

                progressDialog.dismiss();

            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不断收到错误消息:E / RecyclerView:未连接适配器; 跳过布局? - Why do I keep getting the Error: E/RecyclerView: No adapter attached; skipping layout? 我应该如何处理错误的RecyclerView:未连接适配器; 跳过布局 - What Should I Do with the error RecyclerView: No adapter attached; skipping layout 收到错误:recyclerview No adapter attached; 跳过布局 - getting the error : recyclerview No adapter attached; skipping layout 错误提示:未连接适配器; 在recyclerview中跳过布局 - Getting error:No adapter attached; skipping layout in recyclerview RecyclerView错误没有连接适配器; - RecyclerView error No adapter attached; 获取 Null 适配器错误:E/RecyclerView:未连接适配器; 跳过布局 - Getting Null Adapter Error : E/RecyclerView: No adapter attached; skipping layout 我收到此错误“ RecyclerView:未连接适配器; 跳过布局”,但我正在使用“布尔值onCreateOptionsMenu(菜单菜单)” - I'm getting this error “RecyclerView: No adapter attached; skipping layout” but i'm working with “ boolean onCreateOptionsMenu(Menu menu)” 我收到一个错误 E/RecyclerView:没有连接适配器; 跳过布局 - Im getting an error E/RecyclerView: No adapter attached; skipping layout 带有 RecyclerView 的片段,没有适配器附加错误 - Fragment with RecyclerView, no adapter attached error 没有附加适配器使用 RecyclerView 适配器跳过布局错误 - No Adapter Attached Skipping layout error with RecyclerView Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM