Fragment.onCreateView有空集装箱 [英] Fragment.onCreateView has null container
问题描述
下面的是Android 1.6的运行,所以我使用的片段兼容包。在下面的 TestFragment
是一个静态的嵌套类:
公共类FragmentTestActivity扩展FragmentActivity {
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
}
公共静态类TestFragment扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
TextView的结果=新的TextView(getActivity());
result.setText(你好TestFragment);
返回结果;
}
}
}
在main.xml中的文件:
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>
<片段类=com.test.FragmentTestActivity $ TestFragment
机器人:ID =@ + ID /测试
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT/>
< /的FrameLayout>
但奇怪的是,在容器参数 onCreateView
是空
。
现在,如果我编程方式添加的片段像这样(只是改变活动的的onCreate
法)的容器不再为空。为什么呢?
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
片段FRAG =新TestFragment();
。getSupportFragmentManager()的BeginTransaction()加(android.R.id.content,FRAG).commit();
}
说明文档中提到,它可以为null:
大众查看 <一href="http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater%2C%20android.view.ViewGroup%2C%20android.os.Bundle)"相对=nofollow> onCreateView (LayoutInflater 充气,ViewGroup中容器,包savedInstanceState)
[...]
容器: 如果不为null 的,这是父认为, 片段的UI应附。 的片段应该不添加 查看本身的下,但是这可以被用来生成的所述的LayoutParams 视图。
需要明确的是:你不应该做的像什么 container.addView(...)
The following is running on an Android 1.6 so I'm using the compatibility package for fragments. In the following TestFragment
is a static nested class:
public class FragmentTestActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public static class TestFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView result = new TextView(getActivity());
result.setText("Hello TestFragment");
return result;
}
}
}
The main.xml file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<fragment class="com.test.FragmentTestActivity$TestFragment"
android:id="@+id/test"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>
The strange thing is that the container parameter in onCreateView
is null
.
Now, if I add the fragment programatically like so(just change the onCreate
method of the Activity) the container is no longer null. Why?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment frag = new TestFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
}
The documentation mentions that it can be null:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
[...]
container: If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.
To be clear: you shouldn't do anything like container.addView(...)
.
这篇关于Fragment.onCreateView有空集装箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!