android - ChildFragmentManager.findFragmentByID() 在 android 4.4.2 上返回 null

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

我开发了一个应用程序,它使用 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 为 null。看来,返回的 Child FragmentManager 没有找到字段。

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

提前致谢!

编辑: 这是主要 fragment 的 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>

这是子 fragment 的:

<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>

最佳答案

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

所以我决定在 XML 中包含 FrameLayout,并添加嵌套的 通过 FragmentTransaction 的 fragment 。

<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>

在主 fragment 的 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 .

关于android - ChildFragmentManager.findFragmentByID() 在 android 4.4.2 上返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32373278/

相关文章:

android - 如何从 MainActivity 获取当前 fragment

java - 如何在不重新启动广播接收器的情况下将信息从广播接收器发送回我的主要 Activity

android - AVD Manager,为什么我不能创建 AVD?

android - 在 android kitkat 中使用新的 IMMERSIVE 模式

android - Android 应用程序中数据库 Assets 的正确路径

Android viewPager 图片从右向左滑动

java - Android Fragment 上的 ListView 错误

android - ViewPager、FragmentStatePagerAdapter 和无限滚动

Android Fragment - 有关资源 R.id.a_item 定义的查询

android - WebView 缓存在 Android 4.4 中不起作用