我的应用程序工作得很好,当我刚刚有片段的xml中的recyclerview,但我需要 Package 它在constraintlayout的一些原因,但我的应用程序开始崩溃后。
错误消息:
*java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.*
片段代码
public class AdminPostFragment extends Fragment {
RecyclerView recyclerView;
DatabaseReference database;
MyAdapter myAdapter;
ArrayList<Updates> list;
private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_admin_post, container, false);
recyclerView = view.findViewById(R.id.updates_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
FirebaseRecyclerOptions<Updates> options =
new FirebaseRecyclerOptions.Builder<Updates>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("updates"), Updates.class)
.build();
myAdapter = new MyAdapter(options);
recyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
return recyclerView;
}
片段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_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/updates_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
我的适配器代码
package com.project.adminapp.adapter;
import java.util.ArrayList;
.....
public class MyAdapter extends FirebaseRecyclerAdapter< Updates ,MyAdapter.MyViewHolder> {
public MyAdapter(@NonNull FirebaseRecyclerOptions<Updates> options)
{
super(options);
}
@Override
protected void onBindViewHolder(@NonNull final MyViewHolder holder, final int position, @NonNull final Updates model) {
holder.updates_text.setText(model.getUpdate());
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.updates,parent,false);
return new MyViewHolder(v);
}
public class MyViewHolder extends RecyclerView.ViewHolder
{
TextView updates_text ;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
updates_text = (TextView) itemView.findViewById(R.id.updates_text);
}
}
}
当我使用如下代码时:
recyclerview =
(RecyclerView) inflater.inflate(R.layout.fragment_admin_post, container, false);
得到类转换异常ConstraintLayout cannot be cast to recyclerview.
2条答案
按热度按时间yebdmbv41#
您需要修复onCreateView中的返回
xzlaal3s2#
因为ClassCastException正在打印消息,所以您无法将ConstraintLayout转换为RecyclerView。这是因为之前,当您的fragment_admin_post.xml档案中只有recyclerView时,该档案中的根检视是可以转换类型为RecyclerView的回收器检视。现在您已经将它 Package 在ConstraintLayout中,您可以将检视扩充为如下所示:
如果你能显示更详细的代码部分,它会更容易理解。