我有一个片段,我试图添加到一个视图。

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的片段布局。 我上面写的对吗?


当前回答

只需添加viewPager。setOffscreenPageLimit(页面数量);

其他回答

我知道这个问题已经在一种情况下得到了解决,但我的问题略有不同,我想我应该分享一下,以防其他人处于我的处境。

我在onCreate()内做了一个事务,但在这一点上,视图树还没有膨胀,所以你得到这个相同的错误。将事务代码放在onResume()中可以使一切正常运行。

因此,只需确保您的事务代码在视图树膨胀后运行!

我从com.android.support:support-v4:21.0.0升级到com.android.support:support-v4:22.1.1时遇到了这个错误。

我不得不改变我的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container_frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout> 

:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</FrameLayout> 

所以布局必须有一个子视图。我猜他们在新图书馆里强制这么做了。

有点晚了,但可能会对某人有所帮助:

我压倒一切 onCreate (savedInstanceState:包?, persistentState: PersistableBundle? (自动完成首先建议这个,我在这里添加了setContentView())

请确保重写正确的代码: onCreate (savedInstanceState:包?)

我也有这个问题,直到我意识到我在FragmentActivity的onCreate()方法的setContentView()中指定了错误的布局。

传入FragmentTransaction.add()的id,在您的示例中是R.id。feedContentContainer,必须是setContentView()中指定的布局的子元素。

您没有向我们展示onCreate()方法,因此这可能是相同的问题。

在我的例子中,我在回收器视图项中有一个SupportMapFragment(我使用了较低开销的“liteMode”,它使地图看起来是非交互式的,几乎像一个静态图像)。我使用了正确的FragmentManager,一切看起来都很正常……用一个小列表。当项目列表超过屏幕高度时,我在滚动时开始遇到这个问题。

Turned out, it was because I was injecting a dynamic SupportMapFragment inside a view, which was inside another fragment, to get around some issues I was having when trying to declare it statically in my XML. Because of this, the fragment placeholder layout could only be replaced with the actual fragment once the view was attached to the window, i.e. visible on screen. So I had put my code for initialising the SupportMapFragment, doing the Fragment replace, and calling getMapAsync() in the onAttachedToWindow event.

我忘记做的是确保我的代码不会运行两次。例如,在onAttachedToWindow事件中,在尝试创建它的新实例并进行片段替换之前,检查我的动态SupportMapFragment是否仍然为空。当项目离开RecyclerView顶部时,它将从窗口中分离,然后当您回滚到它时重新连接,因此此事件将触发多次。

一旦我添加了空检查,它只发生一次每个RecyclerView项目和问题消失了!TL;博士!