Android fragments IllegalStateException:活动已被销毁。我在单击视图时在适配器类中创建了片段管理器
我创建了recycler视图并执行onclick操作以获得片段视图。当我单击该项时,它应该在片段中显示相同的数据,所以我在适配器类(onBindViewHolder)中创建了一个单击侦听器Android fragments IllegalStateException:活动已被销毁。我在单击视图时在适配器类中创建了片段管理器,android-fragments,commit,fragmentmanager,Android Fragments,Commit,Fragmentmanager,我创建了recycler视图并执行onclick操作以获得片段视图。当我单击该项时,它应该在片段中显示相同的数据,所以我在适配器类(onBindViewHolder)中创建了一个单击侦听器 package.com.example。 公共类适配器扩展 回收视图。适配器{ 私人最终语境; 私人名单; 公共通道适配器(上下文上下文,列表channelList){ this.context=上下文; this.channelList=channelList; } @凌驾 公共通道ViewHolder o
package.com.example。
公共类适配器扩展
回收视图。适配器{
私人最终语境;
私人名单;
公共通道适配器(上下文上下文,列表channelList){
this.context=上下文;
this.channelList=channelList;
}
@凌驾
公共通道ViewHolder onCreateViewHolder(视图组父级,int-viewType){
LayoutInflater LayoutInflater=LayoutInflater.from(上下文);
视图=布局更平坦。充气(R.layout.layout\u视图,父视图,false);
返回新的Channel ViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull final ChannelViewHolder holder,final int position){
最终ChannelModel ChannelModel=channelList.get(位置);
holder.channel_name.setText(channelModel.getChannel_name());
holder.description.setText(channelModel.getDescription());
将(channelModel.getLogo())加载到(holder.logo)中;
holder.itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(v.getContext(),“Clicked”,Toast.LENGTH_LONG.show();
Fragment Fragment=新的FragmentView();
AppCompatActivity活动=新的AppCompatActivity();
FragmentManager FragmentManager=activity.getSupportFragmentManager();
FragmentTransaction FragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.framelayout,fragment).addToBackStack(null);
fragmentTransaction.commit();
}
});
}
@凌驾
public int getItemCount(){
返回channelList.size();
}
公共类ChannelViewHolder扩展了RecyclerView.ViewHolder{
ImageView徽标;
TextView频道名称;
文本视图描述;
公共通道视图持有者(@NonNull final View itemView){
超级(项目视图);
logo=itemView.findviewbyd(R.id.imageView);
channel_name=itemView.findviewbyd(R.id.textViewName);
description=itemView.findviewbyd(R.id.textViewDesc);
}
}
}
单击recyclerview项时查看片段。不清楚您在问什么。如果我们不理解这个问题,就不会有好的答案。请参阅在fragmenttransaction.commit()中获取异常。
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="@+id/recyclerView" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>