简体   繁体   English

没有附加适配器使用 RecyclerView 适配器跳过布局错误

[英]No Adapter Attached Skipping layout error with RecyclerView Adapter

These are steps I am doing in this project:这些是我在这个项目中做的步骤:

  • First I am creating a user profile on Firebase首先,我在 Firebase 上创建用户配置文件
  • Once the profile is created, I am redirecting the user to another activity, where the user is creating a journal with Image创建配置文件后,我将用户重定向到另一个活动,用户在其中创建带有图像的日志
  • Then saving the journal with image on Firestore然后将带有图像的日志保存在 Firestore 上
  • Once the data is created on the user profile, I am trying to fetch the values from Firestore using RecyclerView Adapter在用户配置文件上创建数据后,我尝试使用 RecyclerView Adapter 从 Firestore 获取值
  • Then on a listing page I am trying to fetch all the values in an ArrayList--> here I am getting the error of No Adapter Attached然后在列表页面上,我试图获取 ArrayList 中的所有值-->在这里我收到 No Adapter Attached 的错误

The ArrayList Activity file: ArrayList 活动文件:

  • Initializing the recyclerView in OnCreate在 OnCreate 中初始化 recyclerView

     @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_journal_list); firebaseAuth= FirebaseAuth.getInstance(); user= firebaseAuth.getCurrentUser(); noJournalEntry= findViewById(R.id.list_no_thoughts); journalList= new ArrayList<>(); recyclerView= (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setHasFixedSize(true); }
  • Invoking the RecyclerView in onStart:在 onStart 中调用 RecyclerView:

     @Override protected void onStart() { super.onStart(); //we are getting the user Id of the person who is logged in collectionReference.whereEqualTo("userId", JournalApi.getInstance() .getUserId()) .get() .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() { @Override public void onSuccess(QuerySnapshot queryDocumentSnapshots) { if(!queryDocumentSnapshots.isEmpty()) { for(QueryDocumentSnapshot journals: queryDocumentSnapshots){ Journal journal = journals.toObject(Journal.class); journalList.add(journal); Toast.makeText(JournalListActivity.this,"UserId found", Toast.LENGTH_LONG).show(); } // Invoke Recyclerview journalRecyclerAdapter= new JournalRecyclerAdapter(JournalListActivity.this, journalList); recyclerView.setAdapter(journalRecyclerAdapter); journalRecyclerAdapter.notifyDataSetChanged(); } else { noJournalEntry.setVisibility(View.VISIBLE); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Toast.makeText(JournalListActivity.this,"UserId NOT found", Toast.LENGTH_LONG).show(); } }); }

    } }

As you can see that the RecyclerView is called fine, but the ArrayList is not loading.正如您所看到的, RecyclerView 被调用得很好,但 ArrayList 没有加载。

The Error : E/RecyclerView: No adapter attached; skipping layout错误E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No adapter attached; skipping layout

I referred to various posts in Stackoverflow and followed the steps, not sure where I am going wrong.我参考了 Stackoverflow 中的各种帖子并按照步骤操作,不确定我哪里出错了。

You are getting the following warning, not an error:您收到以下警告,而不是错误:

E/RecyclerView: No adapter attached; E/RecyclerView:没有附加适配器; skipping layout跳过布局

Because you are setting the adapter in a background thread.因为您是在后台线程中设置适配器。 To solve this, you have to set the adapter outside the callback and inside it, just notify it about the changes.要解决此问题,您必须在回调外部和内部设置适配器,只需将更改通知它即可。 So please move the following lines of code:因此,请移动以下代码行:

journalRecyclerAdapter= new JournalRecyclerAdapter(JournalListActivity.this, journalist);
recyclerView.setAdapter(journalRecyclerAdapter);

Right after:紧随其后:

super.onStart();

And leave the following line as it is inside the callback:并将以下行保留在回调中:

journalRecyclerAdapter.notifyDataSetChanged();

And the warning will disapear.并且警告将消失。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM