我有一个片段,我试图添加到一个视图。
FragmentManager fragMgr=getSupportFragmentManager();
feed_parser_activity content = (feed_parser_activity)fragMgr
.findFragmentById(R.id.feedContentContainer);
FragmentTransaction xaction=fragMgr.beginTransaction();
if (content == null || content.isRemoving()) {
content=new feed_parser_activity(item.getLink().toString());
xaction
.add(R.id.feedContentContainer, content)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
Log.e("Abstract", "DONE");
}
当这段代码执行时,我在调试中得到以下错误。
java.lang.IllegalArgumentException: No view found for id 0x7f080011
for fragment feed_parser_activity{41882f50 #2 id=0x7f080011}
feed_parser_activity是一个片段,在xml中被设置为片段布局。
我使用FragmentActivity来托管持有feed_parser_layout的片段布局。
我上面写的对吗?
在我的情况下,我试图显示一个包含页导航的DialogFragment,当FragmentPagerAdapter试图将片段添加到页导航时,这个异常被抛出。根据howettl的回答,我猜这是由于寻页器的父母不是在setContentView()在我的FragmentActivity的视图设置。
我唯一做的改变来解决这个问题是创建FragmentPagerAdapter通过调用getChildFragmentManager()获得的FragmentMager,而不是通过调用getFragmentManager()获得的,因为我通常这样做。
public class PagerDialog extends DialogFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.pager_dialog, container, false);
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getChildFragmentManager());
ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setAdapter(pagerAdapter);
return rootView;
}
}
这个页面似乎是发布关于片段IllegalArgumentException的建议的一个很好的中心位置。还有一件事你可以试试。这是最后对我有用的方法:
我忘了我有一个单独的横向布局文件。在我添加了我的FrameLayout容器之后,这个片段也工作了。
On a separate note, if you have already tried everything else suggested on this page (and the entire Internet, too) and have been pulling out your hair for hours, consider just dumping these annoying fragments and going back to a good old standard layout. (That's actually what I was in the process of doing when I finally discovered my problem.) You can still use the container concept. However, instead of filling it with a fragment, you can use the xml include tag to fill it with the same layout that you would have used in your fragment. You could do something like this in your main layout:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/former_fragment_layout" />
</FrameLayout>
其中former_fragment_layout是您试图在片段中使用的XML布局文件的名称。有关更多信息,请参见使用include重用布局。
如果您试图用fragmentManager替换片段中的片段,但是您没有膨胀父片段,这可能会导致问题。
在BaseFragment.java中OnCreateView:
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.replace(R.id.container, new DifferentFragment())
.commit();
}
return super.onCreateView(inflater, container, savedInstanceState);
取代超级。onCreateView(膨胀器,容器,savedInstanceState);
通过膨胀片段的正确布局:
return inflater.inflate(R.layout.base_fragment, container, false);