繁体   English   中英

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

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

这些是我在这个项目中做的步骤:

  • 首先,我在 Firebase 上创建用户配置文件
  • 创建配置文件后,我将用户重定向到另一个活动,用户在其中创建带有图像的日志
  • 然后将带有图像的日志保存在 Firestore 上
  • 在用户配置文件上创建数据后,我尝试使用 RecyclerView Adapter 从 Firestore 获取值
  • 然后在列表页面上,我试图获取 ArrayList 中的所有值-->在这里我收到 No Adapter Attached 的错误

ArrayList 活动文件:

  • 在 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); }
  • 在 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(); } }); }

    }

正如您所看到的, RecyclerView 被调用得很好,但 ArrayList 没有加载。

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

我参考了 Stackoverflow 中的各种帖子并按照步骤操作,不确定我哪里出错了。

您收到以下警告,而不是错误:

E/RecyclerView:没有附加适配器; 跳过布局

因为您是在后台线程中设置适配器。 要解决此问题,您必须在回调外部和内部设置适配器,只需将更改通知它即可。 因此,请移动以下代码行:

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

紧随其后:

super.onStart();

并将以下行保留在回调中:

journalRecyclerAdapter.notifyDataSetChanged();

并且警告将消失。

暂无
暂无

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

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