一.传参问题
给fargment传数据的时候,不可使用构造方法传递参数。当APP从竖屏到横屏时,会造成数据和理想的不一样,或者造成APP崩溃。原因是从竖屏到横屏这个阶段 Activity正在重构,它所管理的FragmentManager也会被重构,实例化的值丢失。所以在给Fragment传递参数的时候需要使用 Fragment的setArguments()方法传递参数。
public static Fragment newInstance(String arg) {
OneFragmentActivity fragment = new OneFragmentActivity();
Bundle bundle = new Bundle();
bundle.putString("sss",arg);
fragment.setArguments(bundle);
return fragment;
}
OneFragmentActivity 是继承了Fargment的类,该方法写在此类里。
下面是在 oncreateView()中获取参数的方法
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.onefragmentactivity, container,
false);
text = (TextView) view.findViewById(R.id.textView1);
text.setText(getArguments().getString("sss"));
return view;
}
二. java.lang.StackOverflowError
在开发过程中遇到了这个错误,仔细找下来发现是在继承Fragment的类中 一个布局调用方法的问题。
inflater.inflate(R.layout.onefragmentactivity, container);
感觉上是没问题的 但是关于这个方法还有个参数
View android.view.LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)
当你使用View android.view.LayoutInflater.inflate(int resource, ViewGroup root)的时候,默认 attachToRoot 为true,也就是附加到布局文件视图上,因此造成java.lang.StackOverflowError。
解决方法 只需要设置 attachToRoot为false即可。
唉,你们是谁。
我就是白云城主 叶孤城。
还有我冷如冰 寒如雪 剑客西门吹雪
还有我 风流小盲侠 花满楼
还有我 四条眉毛 人见人爱 陆小凤