FragmentManager findFragmentById返回null [英] FragmentManager findFragmentById return null

查看:61
本文介绍了FragmentManager findFragmentById返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

activity_fragment.xml的代码:

The code of activity_fragment.xml:

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

CrimeListActivity.java的代码

The code of CrimeListActivity.java

package com.sinory.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;

public class CrimeListActivity extends SingleFragmentActivity {

    @Override
    protected Fragment createFragment() {
        return new CrimeListFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d("CrimeListActivity", "create");
    }

}

SingleFragmentActivity.java代码

The code of SingleFragmentActivity.java

package com.sinory.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

public abstract class SingleFragmentActivity extends FragmentActivity {

    protected abstract Fragment createFragment();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        if (null == fragment) {
            fragment = new CrimeListFragment();
            fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
        }
    }
}

刚开始学习Android.为什么Fragment片段= fm.findFragmentById(R.id.fragmentContainer)为空?请帮助我.

Just started to learn Android. Why Fragment fragment = fm.findFragmentById(R.id.fragmentContainer) is null? Please help me.

推荐答案

首先,您不需要检查片段是否为null,因为当活动销毁时,所有片段也会被销毁.因此,在活动onCreate中将没有片段,您需要在每次重新创建活动时将其添加.
第二,不要使用findFragmentById.仅当在xml文件中定义片段时才使用此方法.现在,您已经动态添加了片段,您需要指定一个字符串标签,然后使用该标签查找该片段.

First, you don't need to check if the fragment is null because when an activity destroys all the fragment will also be destroyed. So in activity onCreate there will be no fragment and you need to add it each time the activity recreates.
Second don't use findFragmentById. You only use this method when you define your fragment inside your xml file. Now that you dynamically adding your fragment you need to specify a string tag and then use that tag to find the fragment.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);

    FragmentManager fm = getSupportFragmentManager();
    fm.beginTransaction().add(R.id.fragmentContainer, fragment,"TagName").commit();
}

如果您需要查找片段,只需使用以下代码

if you need to find your fragment simply use this code

Fragment fragment = fm.findFragmentByTag("TagName");


2019年1月9日更新
从2018年开始,您可以简单地使用Google导航库在应用程序的页面(片段)之间进行导航.它将处理所有交易,并使导航变得更加轻松和整洁.在这里查看 https://developer.android.com/topic/libraries/architecture/navigation/

这篇关于FragmentManager findFragmentById返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆