片段尚未附加? [英] Fragment has not been attached yet?
问题描述
下面是我的代码.我要达到的目的是在Recycler视图中显示Viewholder.内部视图传呼机中,我正在显示一个片段,向左滑动时,我正在显示另一个片段.但是,当我运行该应用程序时.应用程式当机.不知道我要去哪里.我认为这是片段概念中的某些地方.请帮助我
Below is my code. What i am trying to achieve is that i am displaying viewholder inside my Recycler view. Inside view pager i am displaying one fragment and on swipe left i am displaying another fragment. But when i run the app. App is crashing.Don't know where i am going wrong. I think it's some where in fragment's concept. Please do help me
@Override
public void onBindViewHolder(ManageCustomerViewHolder holder, int position)
{
holder.viewPager.setAdapter(new MyPageAdapter(fragmentManager, fragments));
}
private List<Fragment> getFragments()
{
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(MyFragment.newInstance("Fragment 1"));
fList.add(Myfragment1.newInstance("Fragment 2"));
return fList;
}
public class ManageCustomerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
ViewPager viewPager;
viewPager = (ViewPager) itemView.findViewById(R.id.viewpager);
itemView.setOnClickListener(this);
}
这就是错误所在:
java.lang.IllegalStateException:片段尚未附加.
java.lang.IllegalStateException: Fragment has not been attached yet.
推荐答案
我在我的应用中遇到了同样的问题.
I was facing the same issue in my app.
堆栈跟踪:
java.lang.IllegalStateException: Fragment has not been attached yet. android.support.v4.app.Fragment.instantiateChildFragmentManager(Fragment.java:2154)
android.support.v4.app.Fragment.getChildFragmentManager(Fragment.java:704)
该应用在此行崩溃:
function performSomeActionInChildFragments() {
List<Fragment> fragments = getChildFragmentManager().getFragments();
...
}
当片段不再连接到其父活动/片段时,就会发生这种情况.因此,简单的isAdded()
检查应该可以为您解决此问题.
This happens when the fragment is no longer attached to its parent activity/fragment. So a simple isAdded()
check should resolve this issue for you.
修复:
function performSomeActionInChildFragments() {
if (!isAdded()) return;
List<Fragment> fragments = getChildFragmentManager().getFragments();
...
}
来自文档:
iwego()-如果该片段当前已添加到其活动中,则返回true.
isAdded() - Return true if the fragment is currently added to its activity.
这篇关于片段尚未附加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!