Android Java RecyclerView 错误:未连接适配器;跳过布局[重复]
Posted
技术标签:
【中文标题】Android Java RecyclerView 错误:未连接适配器;跳过布局[重复]【英文标题】:Android Java RecyclerView Error: No adapter attached; skipping layout [duplicate] 【发布时间】:2021-08-29 17:28:02 【问题描述】:我正在尝试显示使用该应用程序的用户关注的所有用户帖子,并且我正在使用 Firestore。我把所有的 id 放在一个数组列表中并构建一个查询。我正在使用 FirebaseRecyclerView,但出现此错误:
2021-06-13 16:33:29.177 22744-22744/com.conta.pophome E/RecyclerView: No adapter attached; skipping layout
我不认为这是由于 java 代码的错误(我不完全确定,但我认为是),但我认为这是由于图形 (xml)。
图形代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity"
>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="@color/primaryPopHome"
android:minHeight="?attr/actionBarSize"
android:theme="@style/AppTheme.bar">
<ImageView
android:layout_
android:layout_
android:scaleType="centerInside"
android:src="@drawable/poplogo"
/>
<androidx.cardview.widget.CardView
android:layout_
android:layout_
android:layout_marginLeft="300dp"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
app:cardCornerRadius="80dp">
<ImageView
android:id="@+id/img_profilomain"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:backgroundTint="@color/white"
android:scaleType="centerCrop"
/>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_fab"
android:layout_
android:layout_
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="@drawable/poplike"
android:backgroundTint="@color/redButton2"
app:borderWidth="0dp"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<!--Floating action button for add alarm-->
<!--Make sure that you are constraining this
button to the parent button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_alarm_fab"
android:layout_
android:layout_
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/add_fab"
app:layout_constraintEnd_toEndOf="@+id/add_fab"
app:layout_constraintStart_toStartOf="@+id/add_fab"
app:srcCompat="@drawable/ic_baseline_add_24"
app:borderWidth="0dp"
android:backgroundTint="@color/redButton2"
/>
<!--Action name text for the add alarm button-->
<!--Make sure that you are constraining this Text to
the add Alarm FAB button-->
<TextView
android:id="@+id/add_alarm_action_text"
android:layout_
android:layout_
android:layout_marginEnd="8dp"
android:text="Crea Post"
app:layout_constraintBottom_toBottomOf="@+id/add_alarm_fab"
app:layout_constraintEnd_toStartOf="@+id/add_alarm_fab"
app:layout_constraintTop_toTopOf="@+id/add_alarm_fab" />
<!--Floating action button for add person-->
<!--Make sure that you are constraining this
button to the add Alarm FAB button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_person_fab"
android:layout_
android:layout_
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/add_alarm_fab"
app:layout_constraintEnd_toEndOf="@+id/add_alarm_fab"
app:layout_constraintStart_toStartOf="@+id/add_alarm_fab"
app:srcCompat="@drawable/ic_baseline_search_24"
app:borderWidth="0dp"
android:backgroundTint="@color/redButton2"
/>
<!--Action name text for the add person button-->
<!--Make sure that you are constraining this Text
to the add Person FAB button-->
<TextView
android:id="@+id/add_person_action_text"
android:layout_
android:layout_
android:layout_marginEnd="8dp"
android:text="Cerca utenti"
app:layout_constraintBottom_toBottomOf="@+id/add_person_fab"
app:layout_constraintEnd_toStartOf="@+id/add_person_fab"
app:layout_constraintTop_toTopOf="@+id/add_person_fab" />
<Button
android:id="@+id/btn_logout"
android:layout_
android:layout_
android:layout_marginStart="152dp"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/appBarLayout"
app:layout_constraintVertical_bias="0.111" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcViewPostMain"
android:layout_
android:layout_
android:layout_marginTop="56dp"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
tools:layout_editor_absoluteX="-16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是java代码:
public class MainActivity extends AppCompatActivity
private FirebaseAuth fAuth;
private DatabaseReference dbRef;
private DatabaseReference uri;
private RecyclerView findPost;
FloatingActionButton mAddFab, mAddAlarmFab, mAddPersonFab;
// These are taken to make visible and invisible along
// with FABs
TextView addAlarmActionText, addPersonActionText;
// to check whether sub FAB buttons are visible or not.
Boolean isAllFabsVisible;
private ArrayList<String> Uidrecord = new ArrayList<>();
private ArrayList<String> uidFollowing = new ArrayList<>();
/*
private String [] Uidrecord = new String[1000];
private String[] uidFollowing = new String[1000];*/
int cont = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
overridePendingTransition(0, 0);
findPost = findViewById(R.id.rcViewPostMain);
fAuth = FirebaseAuth.getInstance();
// Register all the FABs with their IDs
// This FAB button is the Parent
mAddFab = findViewById(R.id.add_fab);
// FAB button
mAddAlarmFab = findViewById(R.id.add_alarm_fab);
mAddPersonFab = findViewById(R.id.add_person_fab);
// Also register the action name text, of all the FABs.
addAlarmActionText = findViewById(R.id.add_alarm_action_text);
addPersonActionText = findViewById(R.id.add_person_action_text);
// Now set all the FABs and all the action name
// texts as GONE
mAddAlarmFab.setVisibility(View.GONE);
mAddPersonFab.setVisibility(View.GONE);
addAlarmActionText.setVisibility(View.GONE);
addPersonActionText.setVisibility(View.GONE);
// make the boolean variable as false, as all the
// action name texts and all the sub FABs are invisible
isAllFabsVisible = false;
// We will make all the FABs and action name texts
// visible only when Parent FAB button is clicked So
// we have to handle the Parent FAB button first, by
// using setOnClickListener you can see below
mAddFab.setOnClickListener(
new View.OnClickListener()
@Override
public void onClick(View view)
if (!isAllFabsVisible)
// when isAllFabsVisible becomes
// true make all the action name
// texts and FABs VISIBLE.
mAddAlarmFab.show();
mAddPersonFab.show();
addAlarmActionText.setVisibility(View.VISIBLE);
addPersonActionText.setVisibility(View.VISIBLE);
// make the boolean variable true as
// we have set the sub FABs
// visibility to GONE
isAllFabsVisible = true;
else
// when isAllFabsVisible becomes
// true make all the action name
// texts and FABs GONE.
mAddAlarmFab.hide();
mAddPersonFab.hide();
addAlarmActionText.setVisibility(View.GONE);
addPersonActionText.setVisibility(View.GONE);
// make the boolean variable false
// as we have set the sub FABs
// visibility to GONE
isAllFabsVisible = false;
);
// below is the sample action to handle add person
// FAB. Here it shows simple Toast msg. The Toast
// will be shown only when they are visible and only
// when user clicks on them
mAddPersonFab.setOnClickListener(
new View.OnClickListener()
@Override
public void onClick(View view)
startActivity(new Intent(MainActivity.this, ricerca.class));
);
// below is the sample action to handle add alarm
// FAB. Here it shows simple Toast msg The Toast
// will be shown only when they are visible and only
// when user clicks on them
mAddAlarmFab.setOnClickListener(
new View.OnClickListener()
@Override
public void onClick(View view)
startActivity(new Intent(MainActivity.this, posta.class));
);
if(fAuth.getCurrentUser() == null)
startActivity(new Intent(MainActivity.this, accesso.class));
return;
ImageView img_profilo = findViewById(R.id.img_profilomain);
dbRef = FirebaseDatabase.getInstance().getReference("Users");
uri = dbRef.child(fAuth.getCurrentUser().getUid()).child("url");
Button btn_logout = findViewById(R.id.btn_logout);
btn_logout.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
fAuth.signOut();
startActivity(new Intent(MainActivity.this, accesso.class));
);
showImage();
FirebaseFirestore.getInstance().collection("following").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("userFollowing").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task)
if (task.isSuccessful())
for (QueryDocumentSnapshot document : task.getResult())
document.getData();
Uidrecord.add(cont,String.valueOf(document.getData()));
cont++;
int conta = 0;
for (String item: Uidrecord)
if(item == null)
break;
uidFollowing.add(conta,getFollowingUid(item));
Toast.makeText(MainActivity.this, uidFollowing.get(conta), Toast.LENGTH_SHORT).show();
conta++;
else
Toast.makeText(MainActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
);
findPost.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
onStart();
@Override
protected void onStart()
super.onStart();
Query query = FirebaseFirestore.getInstance()
.collection("post/" + uidFollowing + "/userPosts")
.limit(1000);
FirestoreRecyclerOptions<GetInfoPost> options = new FirestoreRecyclerOptions.Builder<GetInfoPost>()
.setQuery(query, GetInfoPost.class)
.build();
FirestoreRecyclerAdapter<GetInfoPost, FindFriendsViewHolder> firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<GetInfoPost, FindFriendsViewHolder>(options)
@Override
protected void onBindViewHolder(@NonNull FindFriendsViewHolder holder, int position, @NonNull GetInfoPost model)
holder.txtTitoloInPost.setText(model.getTitle() + " " + model.getValutation() + "/10");
holder.txtData.setText(model.getdate());
Picasso.get().load(model.getUrlImage()).into(holder.imgPost);
holder.txtDescrizione.setText(model.getDescription());
holder.txtTag.setText(model.getGenre());
@NonNull
@Override
public FindFriendsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post, parent, false);
return new FindFriendsViewHolder(view);
;
findPost.setAdapter(firestoreRecyclerAdapter);
firestoreRecyclerAdapter.startListening();
public static class FindFriendsViewHolder extends RecyclerView.ViewHolder
TextView nameInPost, txtData, txtTag, txtTitoloInPost, txtLikenmb, txtDescrizione;
ImageView img_profinPost, imgPost, imgLike;
RelativeLayout cardView;
GifImageView gifLike;
public FindFriendsViewHolder(@NonNull View itemView)
super(itemView);
nameInPost = itemView.findViewById(R.id.nameInPost);
txtData = itemView.findViewById(R.id.txtData);
txtTag = itemView.findViewById(R.id.txtTag);
txtTitoloInPost = itemView.findViewById(R.id.txtTitoloInPost);
txtLikenmb = itemView.findViewById(R.id.txtLikenmb);
txtDescrizione = itemView.findViewById(R.id.txtDescrizione);
img_profinPost = itemView.findViewById(R.id.img_profinPost);
imgPost = itemView.findViewById(R.id.imgPost);
cardView = itemView.findViewById(R.id.card_viewR);
gifLike = itemView.findViewById(R.id.gifLike);
imgLike = itemView.findViewById(R.id.imgLike);
@Override
public void onBackPressed()
private void showImage() //mostro l'immagine usando picasso con l'url nel child url del profilo
ImageView img_prof = findViewById(R.id.img_profilomain);
uri.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
String url = snapshot.getValue(String.class);
// Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show();
Picasso.get().load(url).into(img_prof);
@Override
public void onCancelled(@NonNull DatabaseError error)
);
private String getFollowingUid(String result)
int stringLength = result.length();
String tmp = result.substring(7, stringLength-1);
return tmp;
我尝试阅读其他类似问题,但无法查明问题的原因。 你能帮我理解我做错了什么吗?
修改: 我尝试将回收器视图适配器放入 onCreate 但它仍然不起作用 -> https://codeshare.io/wndbEB
【问题讨论】:
尝试在onCreate
而不是onStart
中设置回收器视图适配器。
@Spidey 刚试过,但没用
check here
@androidLearner 我的代码应该没问题,因为它与我在其他活动中出于类似目的使用过的相同但略有修改,并且可以在那里工作。这就是我认为图形存在问题的原因
findPost.setLayoutManager(new LinearLayoutManager(getApplicationContext()));传递“this”而不是 getApplicationContext。为什么在 onCreate 中调用 onStart()。
【参考方案1】:
您需要在主线程上设置您的recyclerView
。尝试将recyclerView
放入onCreate()
并将.startListening()
放入onStart
。
【讨论】:
我试过了,这里是你说的codeshare.io/78rBQj修改后的代码,但是没办法,错误总是存在 您是否尝试过将您的适配器声明为您班级的一个字段? 我就这样解决了。非常感谢您的帮助 我很乐意提供帮助 :)以上是关于Android Java RecyclerView 错误:未连接适配器;跳过布局[重复]的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView ViewPager java.lang.NullPointerException android.support.v7.widget.RecyclerView$ItemAni
java Android - RecyclerView项目的项目装饰器(分隔符)。
java Android - RecyclerView项目的项目装饰器(分隔符)。