【问题标题】:ChildFragmentManager.findFragmentByID() returns null on android 4.4.2ChildFragmentManager.findFragmentByID() 在 android 4.4.2 上返回 null
【发布时间】:2015-09-03 10:36:09
【问题描述】:

我开发了一个应用,它使用 Fragment 作为 Fragment 的一部分。

public class LoginFragment extends Fragment
   EditTextWithCameraButtonFrag userNameFrag;
   EditTextWithCameraButtonFrag passwordFrag;

 public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);
     userNameFrag = (EditTextWithCameraButtonFrag)getChildFragmentManager()
            .findFragmentById(R.id.fragment_username);
     passwordFrag = (EditTextWithCameraButtonFrag) getChildFragmentManager()
            .findFragmentById(R.id.fragment_password);

EditTextWithCameraButtonFrag 是 Fragment 的子类型。 这段代码就像一个魅力,在 android 5.1.1 上运行。 但是,在 Android 4.4.2 上运行它会返回 userNameFrage 和 passwordFrag 为空。所以看起来,返回的 Child FragmentManager 没有找到这些字段。

在使用getChildFragmentManager()with 4.4.3 时有什么需要考虑的吗?

提前致谢!

编辑: 这是主要片段的 XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/huge"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/login_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="40dp" />

    <fragment
        android:id="@+id/fragment_username"
        android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/fragment_password"
        android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />



    <Button
        android:id="@+id/login_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/huge"
        android:layout_marginStart="@dimen/huge"
        android:layout_marginTop="@dimen/normal"
        android:padding="@dimen/half"
        android:text="Login" />
</LinearLayout>

这是子片段的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editText_with_button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<EditText
    android:id="@+id/text_input_field"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:alpha="0.75"
    android:background="@drawable/edit_text_login_top"
    android:inputType="text"
    android:padding="@dimen/normal" />

<Button
    android:id="@+id/scan_text_view"
    android:layout_width="wrap_content"
    android:background="@android:color/transparent"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/text_input_field"
    android:layout_alignTop="@+id/text_input_field"
    android:layout_alignBottom="@+id/text_input_field"
   />

</RelativeLayout>

【问题讨论】:

    标签: android android-fragments android-4.4-kitkat android-5.1.1-lollipop child-fragment


    【解决方案1】:

    我刚刚解决了它- 有点。 经过大量阅读后,似乎可以通过 XML 声明嵌套片段,但这不是最佳实践。

    所以我决定在 XML 中包含 FrameLayouts,并添加嵌套 片段来自FragmentTransaction

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/huge"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/login_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="40dp" />        
    
        <FrameLayout 
            android:id="@+id/fragment_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <FrameLayout
            android:id="@+id/fragment_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/login_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/huge"
            android:layout_marginStart="@dimen/huge"
            android:layout_marginTop="@dimen/normal"
            android:padding="@dimen/half"
            android:text="Login" />
    </LinearLayout>
    
    </RelativeLayout>
    

    并且在主片段的onCreate中:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // create userNameFrag
        EditTextWithCameraButtonFrag userfragment = new EditTextWithCameraButtonFrag();     
        getChildFragmentManager().beginTransaction().replace(R.id.fragment_username, userfragment).commit();
        // create passwordFrag
        EditTextWithCameraButtonFrag passfragment = new EditTextWithCameraButtonFrag();     
        getChildFragmentManager().beginTransaction().replace(R.id.fragment_password, passfragment).commit();
    
    }
    

    这似乎工作得很好,而且 - 另一个优点 - 我也摆脱了 the duplicate id bug

    【讨论】:

      【解决方案2】:

      从代码中不清楚这两个片段是在哪里添加的:

      1) 在活动中添加(在 xml 或 onCreate 方法中)

      然后这个片段被关联到属于 Activity 的片段管理器。你应该使用getActivity().getFragmentManager().find...

      2) fragemnt xml布局中添加的fragment

      然后在调用片段onViewCreated方法后就可以使用了

      getChildFragmentManager() 用于在此片段的 xml 布局中定义的片段或由此getChildFragmentManager() 片段管理器添加的片段。

      【讨论】:

      • 你是对的,对不起。子片段既不是添加到活动的xml中,也不是通过代码添加,而是添加到片段的xml中。
      • 编辑:我将所有内容(除了超级调用)移至 onViewCreated。仍然没有运气。然后我创建了一个方法并将所有内容移到那里。单击我的活动上的按钮时会调用此方法。子片段被正确渲染。但是,当我单击按钮时, getChildFragmentManager.findFragmentByID() 仍然返回 null。运行 Android 5.1.1 仍然可以正常运行,没有任何问题
      • 检查您没有使用支持库 v4 的片段并将其与普通片段混合。当您将 Fragment 添加为 v4 并且在类中您引用了 android antive Fragment 时,可能会发生这种情况。
      • 所有片段都是原生的android.app.Fragment
      • 完成。感谢您的回答!我注意到奇怪的事情:使用 android >5.0 'getChildFragmentManager().findViewById()` 可以按预期找到我的子片段。使用 4.4.2,我必须使用 'getFragmentManager().findViewById()` 来获取它们。为什么?
      猜你喜欢
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 2017-10-15
      • 2014-06-25
      • 1970-01-01
      相关资源
      最近更新 更多