322

我有一个片段要添加到视图中。

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 中设置为 Fragment 布局的 Fragment。
我正在使用 FragmentActivity 来托管包含feed_parser_layout.
我在上面正确编码了吗?

4

40 回答 40

434

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

FragmentTransaction.add()在您的情况下,传入的 idR.id.feedContentContainer必须是 中指定的布局的子级setContentView()

您没有向我们展示您的onCreate()方法,所以也许这是同一个问题。

于 2011-11-16T21:23:49.247 回答
331

当嵌套 Fragment并使用 getSupportFragmentManager() 而不是 getChildFragmentManager() 添加它们时,也会发生此错误。

于 2013-08-09T12:08:42.793 回答
75

解决方案是使用getChildFragmentManager()

代替getFragmentManager()

从片段调用时。如果您从活动中调用该方法,请使用getFragmentManager().

这将解决问题。

于 2015-02-06T07:17:50.697 回答
33

我遇到的另一种情况。如果您使用嵌套片段,请说片段中的 ViewPager,其页面也是片段。

当您在内部片段(ViewPager 的页面)中进行片段事务时,您将需要

FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

getActivity() 是这里的关键。...

于 2015-06-17T07:27:46.427 回答
31

我遇到了这个问题(在代码中构建我的 UI 时),它是由我的ViewPager(显示Fragment的)没有设置 ID 引起的,所以我只是使用pager.setID(id)它然后它就起作用了。

这个页面帮助我弄清楚了。

于 2012-09-19T15:41:43.567 回答
21

在我的情况下,我试图显示一个包含寻呼机的 DialogFragment,并且当 FragmentPagerAdapter 尝试将片段添加到寻呼机时引发了此异常。根据 howettl 的回答,我猜这是由于 Pager 父级不是我的 FragmentActivity 中 setContentView() 中设置的视图。

我为解决该问题所做的唯一更改是创建传递通过调用 getChildFragmentManager() 获得的 FragmentMager 的 FragmentPagerAdapter,而不是像通常那样通过调用 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;
    }
}
于 2013-04-11T17:12:13.147 回答
16

FragmentTransaction.replace(int ID, fragment)如果您传递给的布局 ID存在于其他正在膨胀的布局中,也会发生此异常。确保布局 ID 是唯一的并且它应该可以工作。

于 2012-09-30T09:31:14.860 回答
15

当我遇到这个问题时,我在另一个类似于这个对我有用的线程上阅读的答案涉及布局 xml。

您的 logcat 显示“未找到 id 0x7f080011 的视图”。

打开gen->package->R.java->id然后查找 id 0x7f080011

当我遇到这个问题时,这个 id 属于FrameLayout我的activity_main.xml文件中的一个。

FrameLayout 没有 ID(没有声明android:id = "blablabla")。

确保所有布局中的所有组件都有 ID,尤其是 logcat 中引用的组件。

于 2013-05-30T18:39:20.477 回答
14

使用嵌套片段

对我来说,通过使用getChildFragmentManager()而不是getActivity().getSupportFragmentManager()解决崩溃

java.lang.IllegalArgumentException:没有为 id 找到视图

于 2018-05-04T06:46:59.120 回答
12

当我从升级com.android.support:support-v4:21.0.0com.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> 

所以布局必须有一个子视图。我假设他们在新图书馆中强制执行了这一点。

于 2015-04-28T01:36:54.277 回答
9

我知道这已经针对一种情况得到了解答,但是我的问题略有不同,我想我会分享一下,以防其他人站在我的立场上。

我在 内进行交易onCreate(),但此时视图树尚未膨胀,因此您会收到同样的错误。放入事务代码onResume()使一切运行良好。

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

于 2012-09-04T03:27:55.390 回答
8

在 Recycler View 中使用 Viewpager 时,我遇到了一个令人讨厌的错误。下面是我在特殊情况下遇到的错误。我开始了一个片段,它有一个带有 Viewpager 的 RecyclerView(使用 FragmentStatePagerAdapter)。它运行良好,直到我在 RecyclerView 中单击单元格时切换到不同的片段,然后使用手机的硬件后退按钮导航回来,应用程序崩溃了。

有趣的是,我在同一个 RecyclerView 中有两个 Viewpager,并且两个 Viewpager 都相距大约 5 个单元格(其他在屏幕上不可见,它已关闭)。所以最初我只是将解决方案应用于第一个 Viewpager 并保留另一个原样(Viewpager using Fragments)。

当第一个视图寻呼机可见时,向后导航工作正常。现在,当我向下滚动到第二个然后更改片段并返回时,它崩溃了(第一个也发生了同样的事情)。所以我不得不改变两个Viewpagers。

无论如何,请阅读以下内容以找到可行的解决方案。崩溃错误如下:

java.lang.IllegalArgumentException: No view found for id 0x7f0c0098 (com.kk:id/pagerDetailAndTips) for fragment ProductDetailsAndTipsFragment{189bcbce #0 id=0x7f0c0098}

花了几个小时调试它。阅读这篇完整的 Thread 帖子直到底部应用所有解决方案,包括确保我正在传递 childFragmentManager。

没有任何效果。

最后,我没有使用 FragmentStatePagerAdapter ,而是扩展了 PagerAdapter 并在 Viewpager 中使用它而不使用片段。我相信有些地方有嵌套片段的BUG。无论如何,我们有选择。读 ...

下面的链接非常有帮助:

没有片段的 Viewpager

链接可能会死,所以我在下面发布我实施的解决方案:

public class ScreenSlidePagerAdapter extends PagerAdapter {
private static final String TAG = "ScreenSlidePager";
ProductDetails productDetails;
ImageView imgProductImage;
ArrayList<Imagelist> imagelists;
Context mContext;

// Constructor
public ScreenSlidePagerAdapter(Context mContext,ProductDetails productDetails) {
    //super(fm);
    this.mContext = mContext;
    this.productDetails = productDetails;
}

// Here is where you inflate your View and instantiate each View and set their values
@Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.product_image_slide_cell,container,false);

    imgProductImage = (ImageView) layout.findViewById(R.id.imgSlidingProductImage);
    String url = null;
    if (imagelists != null) {
        url = imagelists.get(position).getImage();
    }

    // This is UniversalImageLoader Image downloader method to download and set Image onto Imageview
    ImageLoader.getInstance().displayImage(url, imgProductImage, Kk.options);

    // Finally add view to Viewgroup. Same as where we return our fragment in FragmentStatePagerAdapter
    container.addView(layout);
    return layout;
}

// Write as it is. I don't know much about it
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
    /*super.destroyItem(container, position, object);*/
}

// Get the count
@Override
public int getCount() {
    int size = 0;

    if (productDetails != null) {
        imagelists =  productDetails.getImagelist();
        if (imagelists != null) {
            size = imagelists.size();
        }
    }
    Log.d(TAG,"Adapter Size = "+size);
    return size;
}

// Write as it is. I don't know much about it
@Override
public boolean isViewFromObject(View view, Object object) {

    return view == object;
}

}

希望这有帮助!

于 2015-10-14T17:58:08.193 回答
8

以防万一有人犯了我犯的同样愚蠢的错误;检查您是否没有在某处覆盖活动内容(即寻找对 setContentView 的其他调用)

就我而言,由于不小心复制和粘贴,我在片段中使用了 DataBindingUtil.setContentView,而不是 DataBindingUtil.inflate,这会弄乱活动的状态。

于 2018-07-25T01:35:42.227 回答
7

我有同样的问题,但我的问题发生在方向改变上。其他解决方案均无效。所以事实证明setRetainInstance(true);,在根据屏幕尺寸进行两个或一个窗格布局时,我忘记从我的片段中删除。

于 2014-01-05T19:40:19.307 回答
5

我的错误是在FragamentTransaction.

我正在这样做t.replace(R.layout.mylayout);而不是t.replace(R.id.mylayout);

不同的是,一个是布局,另一个是参考layout(id)

于 2013-11-12T12:50:28.087 回答
3

我有同样的问题,让我发布我的代码,以便大家都可以看到它,而不是做和我一样的事情。

@Override
protected void onResume()
{
    super.onResume();

    fragManager = getSupportFragmentManager();

    Fragment answerPad=getDefaultAnswerPad();
    setAnswerPad(answerPad);
    setContentView(R.layout.abstract_test_view);
}
protected void setAnswerPad(AbstractAnswerFragment pad)
{
    fragManager.beginTransaction()
        .add(R.id.AnswerArea, pad, "AnswerArea")
        .commit();
    fragManager.executePendingTransactions();
}

请注意,我在setContentView. 哎呀。

于 2012-11-22T02:06:57.967 回答
3

此页面似乎是发布有关 Fragment IllegalArgumentException 建议的一个很好的中心位置。这是您可以尝试的另一件事。这最终对我有用:

我忘记了我有一个单独的横向布局文件。在我在那里添加了我的 FrameLayout 容器之后,该片段也起作用了。


另外,如果您已经尝试了此页面(以及整个 Internet)上建议的所有其他内容并且已经花费了几个小时,请考虑丢弃这些烦人的片段并返回到一个好的旧标准布局。(当我最终发现我的问题时,这实际上是我正在做的事情。)你仍然可以使用容器的概念。但是,您可以使用 xmlinclude标记来使用您在片段中使用的相同布局来填充它,而不是用片段填充它。您可以在主布局中执行以下操作:

<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 布局文件的名称在哪里。有关详细信息,请参阅使用包含重用布局

于 2015-01-02T08:34:23.530 回答
3

我修复了这个错误,我使用了commitNow()replace commit()

mFragment.getChildFragmentManager()
  .beginTransaction()
  .replace(R.id.main_fragment_container,fragment)
  .commitNowAllowingStateLoss();

commitNow是一个同步方法,该commit()方法是一个异步方法。

于 2018-05-04T07:38:37.563 回答
2

就我而言,我在回收站视图项中有一个 SupportMapFragment(我使用的是开销较低的“liteMode”,它使地图看起来像非交互式的,几乎像静态图像)。我使用了正确的 FragmentManager,一切似乎都运行良好......只有一个小列表。一旦项目列表超出屏幕高度一点,我在滚动时就开始遇到这个问题。

原来,这是因为我在视图中注入了一个动态 SupportMapFragment,它位于另一个片段中,以解决我在尝试在我的 XML 中静态声明它时遇到的一些问题。因此,片段占位符布局只能在视图附加到窗口(即在屏幕上可见)后替换为实际片段。因此,我将我的代码用于初始化 SupportMapFragment、进行片段替换以及在 onAttachedToWindow 事件中调用 getMapAsync()。

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

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

于 2017-01-11T23:32:00.960 回答
2

当您从另一个片段中的片段调用时会发生这种情况。

采用 :

getActivity().getSupportFragmentManager().beginTransaction();
于 2018-03-06T06:44:34.437 回答
2

<include layout="@layout/your_fragment_layout"/>当您不放入时也会发生此问题app_bar_main.xml

于 2019-03-31T05:19:57.800 回答
2

使用 childFragmentManager 而不是活动!!.supportFragmentManager

于 2020-06-10T12:15:03.233 回答
2

我在我的项目中使用 View Binding 并且setContentView()在膨胀ActivityHelloWorldBinding类后不注意添加:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityHelloWorldBinding.inflate(layoutInflater)

    // Add this line.
    setContentView(binding.root)
}
于 2020-12-21T09:57:14.907 回答
1

当我尝试用我的片段替换视图时遇到了这个问题onCreateView()。像这样:

public class MyProjectListFrag extends Fragment {


    private MyProjectListFragment myProjectListFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        FragmentManager mFragmentManager = getFragmentManager();
        myProjectListFragment = new MyProjectListFragment();
        mFragmentManager
                .beginTransaction()
                .replace(R.id.container_for_my_pro_list,
                        myProjectListFragment, "myProjectListFragment")
                .commit();
    }

它告诉我

11-25 14:06:04.848: E/AndroidRuntime(26040): java.lang.IllegalArgumentException: No view found for id 0x7f05003f (com.example.myays:id/container_for_my_pro_list) for fragment MyProjectListFragment{41692f40 #2 id=0x7f05003f myProjectListFragment}

然后我通过将替换放入onActivityCreated(). 像这样:

public class MyProjectListFrag extends Fragment {

    private final static String TAG = "lch";

    private MyProjectListFragment myProjectListFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        return inflater
                .inflate(R.layout.frag_my_project_list, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        FragmentManager mFragmentManager = getFragmentManager();
        myProjectListFragment = new MyProjectListFragment();
        mFragmentManager
                .beginTransaction()
                .replace(R.id.container_for_my_pro_list,
                        myProjectListFragment, "myProjectListFragment")
                .commit();

    }
  1. 您必须返回一个视图,onCreateView()以便稍后替换它
  2. 您可以在片段提升循环中的以下函数中对该视图进行任何操作,例如onActivityCreated()

希望这可以帮助!

于 2014-11-25T06:25:06.683 回答
1

在我的情况下,当我为相同的布局元素(片段占位符)使用不同的 id 而同时为不同的 Build Variants 使用不同的 id 时,会引发此异常。出于某种原因,当您第一次替换片段时它工作得很好,但是如果您再次尝试这样做,您会得到这个异常。因此,如果您对不同的构建变体有多个布局,请确保使用相同的 id。

于 2018-04-23T14:02:00.817 回答
1

我遇到了这个问题。在我的情况下,我忘记在我的 XML 文件中添加 FrameLayout,添加框架布局后,我的问题已经解决。

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/wraper"
        android:layout_above="@id/wraper"/>
于 2021-10-22T04:44:46.770 回答
0

如果您尝试用 替换片段中的片段,fragmentManager但您没有膨胀可能导致问题的父片段。

BaseFragment.java 中 OnCreateView

if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, new DifferentFragment())
                    .commit();
        }

return super.onCreateView(inflater, container, savedInstanceState);

替换super.onCreateView(inflater, container, savedInstanceState); 为片段的正确布局:

        return inflater.inflate(R.layout.base_fragment, container, false);
于 2015-02-24T08:52:31.180 回答
0

在创建活动时进行片段事务时,我遇到了同样的问题。

核心问题是尼克已经指出的——视图树还没有被膨胀。但他的解决方案不起作用——onResume、onPostCreate 等中的相同异常。

解决方案是向容器片段添加回调以在它准备好时发出信号:

public class MyContainerFragment extends Fragment {
    public static interface Callbacks {
        void onMyContainerAttached();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d(TAG, "--- onAttach");
        ((Callbacks) activity).onMyContainerAttached();
    }

    //... rest of code
}

然后在活动中:

public class MainActivity extends Activity
        implements MyContainerFragment.Callbacks
{
    @Override
    public void onMyContainerAttached() {
        getFragmentManager()
                .beginTransaction()
                .replace(R.id.containerFrame, new MyFragment())
                .commit();
    }

    //...
}
于 2015-03-05T05:14:29.960 回答
0

在我的例子中,我使用一个片段类文件来声明一个列表视图适配器类。我只是为公共适配器类使用了一个不同的文件,错误就消失了。

于 2016-01-14T23:38:54.260 回答
0

当您在具有相同 ID 的两个片段中有两个视图时,也会发生这种情况

于 2019-04-27T18:11:40.530 回答
0

我遇到了同样的问题,因为我在将容器布局添加到活动之前尝试添加片段。

于 2019-05-14T14:04:12.097 回答
0

有时是因为您使用的是 BottomNavigationView。如果您从导航中打开一个 Intent 并在该活动中打开一个片段,可以说

transaction.replace(R.id.container,new YourFragment());

那么 Activity 将无法找到您正在使用的导航方法。

解决方案:在您的应用程序中使用 addOnBackStack 将活动更改为片段并处理导航。如果您已经实现了 Jetpack Navigation,只需在您的项目中使用片段。

于 2019-10-25T18:15:51.850 回答
0

就我而言。我有一个Activitywith serveral Fragments 有时我需要重新创建Fragments

  • 语言是变化
  • 片段布局有些需要有些不需要或内容更改需要重新创建
  • 其他变化
  • 我清除了所有Fragments 并将所有设置为null活动但 Fragment 已经创建了自己,而它的宿主 activty 是 bean 设置为 null,所以在调用 Fragment 视图之前检查它是否为 null

    例如

    Activity{
        fragment
        recreate{
           fragment = null then new instance
        }
    
    }
    
    Fragment{
        if((activity).fragment != null) {
           findViewById()
        }
    
    }
    
    于 2020-01-16T03:19:54.633 回答
    0

    使用导航库,当NavHostFragment没有 id 时会出现问题。

    错误声明:

    <fragment               
         android:name="androidx.navigation.fragment.NavHostFragment"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         app:navGraph="@navigation/myGraph"/>
    

    正确的声明:

    <fragment
         android:id="@+id/myId"               
         android:name="androidx.navigation.fragment.NavHostFragment"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         app:navGraph="@navigation/myGraph"/>
    
    于 2020-02-04T10:39:54.170 回答
    0

    在我们的例子中,我们已经清除/损坏的class.dex文件以及 gradle 编译的输出。由于没有尝试缓存重新编译,因此没有错误,但是 apk 没有所需的文件导致这个令人困惑的错误。gradle 重新同步和全新构建使我们摆脱了所有错误。

    // 快乐编码

    于 2020-02-19T17:55:42.563 回答
    0

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

    我正在覆盖 onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) (自动完成首先建议了这个,我在这里添加了 setContentView() )

    请确保改写正确的: onCreate(savedInstanceState: Bundle?)

    于 2020-12-23T20:25:58.820 回答
    0

    在我的情况下,我在我的活动类中使用了一个通用片段持有者,我在运行时用适当的片段替换了这个通用片段。

    问题是我给 include 和 generic_fragment_layout 都提供了 id,从 include 中删除 id 解决了它。

    于 2020-12-28T11:51:55.130 回答
    0

    正如https://stackoverflow.com/users/2075875/malachiasz所说

    这个解决方案确实有效

    getChildFragmentManager().beginTransaction().replace(R.id.cpu_bottomNav_frame,new fragment()).commit();
    
    于 2021-07-22T05:48:43.887 回答
    0

    只需添加 viewPager.setOffscreenPageLimit( 页数 );

    于 2021-07-23T12:52:29.927 回答
    -2

    然后我也遇到了同样的错误,我意识到问题出在 .add(R.id.CONTAINERNAME) 所以基本上我在错误的 xml 中给出了 id = CONTAINERNAME .. 所以解决方案是首先检查你有给定 xml 中的 id = CONTAINERNAME 您希望在其中显示片段 .. 另请注意,我试图将片段称为活动 .. 我希望这样的小错误能解决问题

    参考 2020 年最新版本的安卓系统

    于 2020-09-05T11:40:11.267 回答