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
Access Fragment View from Activity's onCreate
提问by Dan
I am in the process of making my first app for Android, and I have a Fragment
that gets added to my Activity
in the Activity
's onCreate()
method. The problem I am facing is that I am unable to find any of the views contained within the Fragment
from the Activity
's onCreate()
method.
我正在为 Android 制作我的第一个应用程序,并且Fragment
我Activity
在Activity
的onCreate()
方法中添加了一个。我现在面临的问题是,我无法找到任何的内部包含的意见,Fragment
从Activity
的onCreate()
方法。
Other threads have suggested that this is because the Fragment
has 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 Fragment
in 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()
.
因为这个命令,任何试图访问的意见Fragment
中Activity
的onCreate()
方法(使用findViewById()
)产生一个NullPointerException
作为Fragment
的onCreateView()
唯一被调用的结束之后Activity
的onCreate()
。
Using the FragmentManger
's executePendingTransactions()
after adding the Fragment
doesn'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 Activity
your Fragment
is attached to, use onActivityCreated()
.
或者,如果您的更改取决于Activity
您Fragment
的附件,请使用onActivityCreated()
.
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView tv = (TextView) getView().findViewById(R.id.text);
tv.setText(getActivity.getSomeText());
}