Java 未调用 onCreateView 片段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25444678/
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
onCreateView Fragment not called
提问by Adriana Carelli
I have a custom DialogFragment
with a FrameLayout
container in which I want to put a Fragment
but its view always return null
, what can I do?
我有一个自定义DialogFragment
与FrameLayout
中,我想提出一个容器Fragment
,但其观点总是返回null
,我该怎么办?
In DialogCreateAccount.java
在 DialogCreateAccount.java
public class DialogCreateAccount extends DialogGeneral implements OnClickListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Dialog dialog = super.onCreateDialog(savedInstanceState);
//........
return dialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Fragment fragment = CreateAccountFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
if(fragment.getView()!=null){ // return always null
ft.add(
getFrameContainer().getId(),
fragment
)
.commit();
}else{
}
Log.i("DialogCreateAccount", "fragment:" +fragment.getView());// return null
}
In CreateAccountFragment.java
在 CreateAccountFragment.java 中
public class CreateAccountFragment extends Fragment implements OnClickListener{
public CreateAccountFragment() {
// TODO Auto-generated constructor stub
}
public static CreateAccountFragment newInstance() {
CreateAccountFragment f = new CreateAccountFragment();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("CreateAccountFragment", "onCreate");
//onCreate is called
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("CreateAccountFragment", "onCreateView");
//onCreateView not called
mRootView = inflater.inflate(R.layout.dialog_create_an_account, container, false);
return mRootView;
}
}
In Fragment onCreate
is called and onCreateView
is not called.
在 Fragment 中onCreate
被调用和onCreateView
不被调用。
采纳答案by laalto
Fragment getView()
only returns a non-null view once onCreateView()
has been run in the fragment lifecycle.
FragmentgetView()
仅onCreateView()
在片段生命周期中运行一次后才返回非空视图。
Merely instantiating a fragment object does not call any of its lifecycle callbacks. They will be called later when the fragment transaction executes.
仅仅实例化一个片段对象不会调用它的任何生命周期回调。稍后将在片段事务执行时调用它们。
Just put the fragment in the container without conditionally checking whether getView()
returns non-null.
只需将片段放入容器中,无需有条件地检查是否getView()
返回非空值。
回答by Okas
Your fragment has to be associated with a view. This can be done either be done in xml layout or programmatically.
您的片段必须与视图相关联。这可以通过 xml 布局或以编程方式完成。
See Fragments documentation, section "Adding fragment to an activity".
请参阅片段文档,“将片段添加到活动”部分。