我正在制作一个日记应用程序,每当我尝试使用工具栏上的菜单时,它都会说没有适配器附加跳过布局,并且不允许我将其他日记添加到我的日记列表(回收器视图)中。
package com.example.self;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.storage.StorageReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import model.Journal;
import ui.JournalRecyclerAdapter;
import util.JournalApi;
public class JournalListActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener authStateListener;
private FirebaseUser user;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private StorageReference storageReference;
private List<Journal> journalList;
private RecyclerView recyclerView;
private JournalRecyclerAdapter journalRecyclerAdapter;
private CollectionReference collectionReference = db.collection("Journal");
private TextView noJournalEntry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_journal_list);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
noJournalEntry = findViewById(R.id.list_no_thoughts);
journalList = new ArrayList<>();
recyclerView = findViewById(R.id.recycleView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.action_add) {
if (user != null && firebaseAuth != null) {
startActivity(new Intent(JournalListActivity.this, PostJournalActivity.class));
finish();
}
} else if (item.getItemId() == R.id.action_signout) {
if (user != null && firebaseAuth != null) {
firebaseAuth.signOut();
startActivity(new Intent(JournalListActivity.this, MainActivity.class));
finish();
}
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
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);
}
// Povikuvanje na 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) {
}
});
}
}
当我在 RecyclerView 上调用 setAdapter() 时,收到错误“未连接适配器;正在跳过布局”。即使我在从 Firestore 获取数据后在 onStart() 方法中设置适配器,也会发生这种情况
我已确保在添加新日记条目后调用notifyDataSetChanged(),并尝试在onCreate()中使用空列表设置适配器,但问题仍然存在。
关于如何解决这个问题有什么建议吗?
您收到以下消息,这是警告而不是错误:
E/RecyclerView:未连接适配器;跳过布局
因为您正在后台线程中的 Firebase
journalRecyclerAdapter
请求的回调中设置 get()
。
要解决此问题,您必须在回调的外部设置适配器并在其中设置适配器,只需通知它有关更改即可。所以请移动以下代码行:
journalRecyclerAdapter = new JournalRecyclerAdapter(JournalListActivity.this, journalList);
recyclerView.setAdapter(journalRecyclerAdapter);
之后:
journalList = new ArrayList<>();
并且在回调内部只保留以下代码行:
journalRecyclerAdapter.notifyDataSetChanged();
除此之外,也请不要再忽视错误。请至少添加:
Log.d("TAG", e.getMessage());
在你的
onFailure
里面。这样,您就可以随时知道读取数据的操作失败了。