Android 从活动的 onCreate 访问片段视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12122624/
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 09:48:19  来源:igfitidea点击:

Access Fragment View from Activity's onCreate

androidfragmentoncreatefindviewbyid

提问by Dan

I am in the process of making my first app for Android, and I have a Fragmentthat gets added to my Activityin the Activity's onCreate()method. The problem I am facing is that I am unable to find any of the views contained within the Fragmentfrom the Activity's onCreate()method.

我正在为 Android 制作我的第一个应用程序,并且FragmentActivityActivityonCreate()方法中添加了一个。我现在面临的问题是,我无法找到任何的内部包含的意见,FragmentActivityonCreate()方法。

Other threads have suggested that this is because the Fragmenthas not yet been inflated, so findViewById()will return null for any views contained within the Fragment.

其他线程建议这是因为Fragment尚未膨胀,因此findViewById()对于包含在Fragment.

Here is what I mean:

这就是我的意思:

Activity:

活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("activity onCreate");
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null) {
        return;
    }

    initialiseUI(); // Fragment added to Activity

    System.out.println("end of activity onCreate");
}

Fragment:

分段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
System.out.println("fragment onCreateView");
    return inflater.inflate(R.layout.event_log, container, false);
}

This prints the results:

这将打印结果:

activity onCreate end of activity onCreate fragment onCreateView

活动 onCreate 活动结束 onCreate 片段 onCreateView

Because of this order, any attempt to access the views of the Fragmentin the Activity's onCreate()method (using findViewById()) produces a NullPointerException, as the Fragment's onCreateView()only gets called AFTER the end of the Activity's onCreate().

因为这个命令,任何试图访问的意见FragmentActivityonCreate()方法(使用findViewById())产生一个NullPointerException作为FragmentonCreateView()唯一被调用的结束之后ActivityonCreate()

Using the FragmentManger's executePendingTransactions()after adding the Fragmentdoesn't help.

添加后使用FragmentManger'没有帮助。executePendingTransactions()Fragment

Basically, I have been forced to put the problem code in the Activity's onStart()method instead of onCreate(), as onStart()happens AFTER the Fragment's onCreateView().

基本上,我被迫将问题代码放在Activity'sonStart()方法中,而不是onCreate()onStart()Fragment's之后发生的那样onCreateView()

Does anyone what the standard practice here is, or how I can make my Fragment-View-accessing code work within the Activity's onCreate()method?

有没有人这里的标准做法是什么,或者我如何让我的 Fragment-View 访问代码在Activity'sonCreate()方法中工作?

采纳答案by biegleux

Update your views in onCreateView().

在 中更新您的观点onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.event_log, container, false);
    TextView tv = (TextView) view.findViewById(R.id.text);
    tv.setText("hello world");
    return view;
}

Or if your changes depend on Activityyour Fragmentis attached to, use onActivityCreated().

或者,如果您的更改取决于ActivityFragment的附件,请使用onActivityCreated().

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

    TextView tv = (TextView) getView().findViewById(R.id.text);
    tv.setText(getActivity.getSomeText());
}