Fragment 中调用findFragmentById 报null,从Fragment的生命周期带你理解

本文探讨了在Fragment中使用findFragmentById可能出现的null问题,通过详细解析Fragment的生命周期,指出在onActivityCreated()之后调用此方法才能确保获取到正确的对象,因为此时Activity的onCreate()已完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先我们在主界面设置两个静态Fragment,布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
    >

    <fragment
        android:id="@+id/item_list"
        android:name="com.example.myapplication.ItemFragment"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        />
    <fragment
        android:id="@+id/item_detail"
        android:name="com.example.myapplication.ItemDetaiFragment"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        />
</LinearLayout>

MainActivity 一启动,这两个Fragment 就会被创建。两个Fragment布局文件如下:

ItemFragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ListFragment"
        />
</LinearLayout>

ItemDetaiFragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
    >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DetailFragment"
        />
</LinearLayout>

在这里我们测试在ItemFragment 去刷新ItemDetaiFragment中的UI(当然这样是不合理的,这里不做讨论)。ItemFragment 如下:

package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2016/8/5.
 */
public class ItemFragment extends Fragment {

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


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

        View view = inflater.inflate(R.layout.item_list_fragment,container,false);
        FragmentManager fm = getFragmentManager();
        final  ItemDetaiFragment itemDetaiFragment = (ItemDetaiFragment) fm.findFragmentById(R.id.item_detail);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                itemDetaiFragment.tv.setText("dsklafjksdafjlksa");
            }
        }, 2000);
        return view ;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);




    }

   
}

ItemDetailFragment :

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/8/5.
 */
public class ItemDetaiFragment extends Fragment {


    public TextView tv ;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.item_detail_fragment,container,false);
        tv = (TextView)view.findViewById(R.id.tv);
        return view ;
    }
}

运行后崩溃!这是怎么回事呢?通过 findFragmentById竟然拿到的对象是null!这一切都要从Fragment的生命周期讲起。



onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
从生命周期来看,Fragment的onActivityCreated方法才是Activity的onCreate方法返回时调用,如果我们在onActivityCreated去findFragmentById ,会不会拿到对应的对象呢?答案是肯定的!也就是说当Activity的OnCreate完成之后,这时候才能拿到相应的对象。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值