Android findFragmentById 总是返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10833666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:09:58  来源:igfitidea点击:

findFragmentById always returns null

androidandroid-fragments

提问by Viper

I'm defining an ID for my fragment in the xml layout:

我在 xml 布局中为我的片段定义了一个 ID:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/test_fragment"
...

Then I add this fragment in the activity's onCreate method:

然后我在活动的 onCreate 方法中添加这个片段:

MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.fragment_container, myFragment);
fragmentTransaction.commit();

This is all working fine. Replacing fragments and is also working.

这一切正常。更换碎片,也在工作。

Later I'm trying to retrieve this fragment by its ID in one of the activity's methods:

后来我尝试在活动的方法之一中通过其 ID 检索此片段:

MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentById(R.id.test_fragment);

Doing so leads to myFragment being null. Always.

这样做会导致 myFragment 成为null. 总是。

When I try to do the same with tags instead of IDs I can retrieve the fragment by its tag without any problems:

当我尝试对标签而不是 ID 执行相同操作时,我可以通过标签检索片段而不会出现任何问题:

MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.fragment_container, myFragment, "testfragment");
fragmentTransaction.commit();

...

...

MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentByTag("testfragment");

Why can't findFragmentById find the fragment, but findFragmentByTag does so? Am I missing something?

为什么 findFragmentById 找不到片段,而 findFragmentByTag 却能找到?我错过了什么吗?

采纳答案by StErMi

R.id.test_fragmentis not the ID of your fragment but the id of your LinearLayout

R.id.test_fragment不是您的片段的 ID,而是您的 LinearLayout 的 ID

Calling add(int containerViewId, Fragment fragment)will add a fragment without a tag. So or you use add(int containerViewId, Fragment fragment, String tag)and you get back your fragment using your tag (as an ID)

调用add(int containerViewId, Fragment fragment)将添加一个没有标签的片段。因此,或者您使用add(int containerViewId, Fragment fragment, String tag)并使用您的标签(作为 ID)取回您的片段

回答by Amit Bhandari

Use the <FrameLayout>tag as a container in your layout file. Later to replace the fragments from your activity dynamically, you can use the ID of the <FrameLayout>container to replace the fragments in your activity.

<FrameLayout>标签用作布局文件中的容器。稍后要动态替换 Activity 中的片段,您可以使用<FrameLayout>容器的 ID替换 Activity 中的片段。

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

回答by vir us

I was using android.support.v4.app.Fragmentin my layout while calling getFragmentManager()which actually searched for android.app.Fragmentsubclasses and I got null. So the fix was to call getSupportFragmentManager()instead.

android.support.v4.app.Fragment在我的布局中使用它,同时调用getFragmentManager()它实际上搜索了android.app.Fragment子类,我得到了null. 所以解决方法是调用getSupportFragmentManager()

In general make sure the package of a fragment you are subclassing and using in your layout is the same returned by the corresponding FragmentManagerwhich performs search.

通常,请确保您子类化并在布局中使用的片段包与FragmentManager执行搜索的相应片段返回的包相同。

回答by Blackbelt

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/test_fragment"

it should be a fragmentnot a LinearLayout

它应该fragment不是一个LinearLayout

<fragment android:name="com.example.yourfragment"
            android:id="@+id/test_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

回答by GBouerat

R.id.test_fragmentis your LinearLayoutID not your Fragment.

R.id.test_fragment是你的LinearLayoutID 不是你的 Fragment。

You can define and id on a fragment when it is inflated from an xml like in this sample http://developer.android.com/guide/topics/fundamentals/fragments.html#Adding

当片段从 xml 膨胀时,您可以在片段上定义和标识,如本示例http://developer.android.com/guide/topics/fundamentals/fragments.html#Adding

回答by user2168109

Or, you should have instead used :

或者,您应该改为使用:

(MyFragment) getFragmentManager().findFragmentById(R.id.fragment_container);

回答by Aman Srii

FragmentB fragmentB = 
(FragmentB) getSupportFragmentManager().findFragmentById(R.id.containerb);

if(fragmentB != null)
{
   fragmentB.showuserResponse(msg);
}

Use container id as fragment id. And then check for null reference.

使用容器 ID 作为片段 ID。然后检查空引用。

回答by Abdul Rahman Shamair

The reasons mentioned above are valid but another possible reason is that you are trying to search for the fragment while being in a fragment this also results in fragmentManager.findFragmentByIdto return null. We should use childFragmentManager.findFragmentByIdto find the fragment inside a fragment. According to the official documentation.

上面提到的原因是有效的,但另一个可能的原因是您试图在片段中搜索片段,这也会导致fragmentManager.findFragmentById返回 null。我们应该使用childFragmentManager.findFragmentById在片段中查找片段。根据官方文档。

public final androidx.fragment.app.FragmentManager getChildFragmentManager()

public final androidx.fragment.app.FragmentManager getChildFragmentManager()

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

返回一个私有 FragmentManager,用于在此 Fragment 中放置和管理 Fragment。

回答by suitianshi

fragmentTransaction.add(R.id.fragment_container, myFragment);

MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentById(R.id.test_fragment);

fragmentTransaction.add(R.id.fragment_container, myFragment);

MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentById(R.id.test_fragment);

Please notice the difference.

请注意区别。

You should pass R.id.fragment_container as the argument to findFragmentById, as you pass it to the add function, instead of R.id.test_fragment

您应该将 R.id.fragment_container 作为参数传递给findFragmentById,因为您将它传递给 add 函数,而不是R.id.test_fragment

By the way , according to the inner implementation of the two functions, it should be right that the id can be that of its container view.

顺便说一下,根据两个函数的内部实现,id可以是它的container view的id应该是对的。