今天本来想研究android设备翻屏之后activity和fragment的是否都要重建,即activtiy和fragment的页面布局需要重新加载。本来想在fragment的扩展类中的onCreateView中加个button空间,然后点击之后向fragment页面添加一个textview,然后翻屏之后看看新添加的textview是否还在。最初不知道怎么在fragment扩展类中使用findViewById();应为一般在acvitity类中可以直接使用findViewById()。后来在知道findViewById()是android.view.View中的方法,fragment不是View的子类,所以不能直接食用findViewById(),需要创建一个view类或其子类(如activity类)的对象才能使用。使用getAcitivty()和getView()获得view对象,由于fragment的onCreateView需要返回个一个该fragment对应的view对象(inflater.inflate() 获取),可以使用该对象来使用findViewById()。
然后我最先使用了在onCreateView使用了getActivity()来使用findViewById():getActivity().findViewById()。然后写了一个监听器,结果程序运行就报错,报错的地方就是监听器的第一行。
然后写写了下面的代码:
public class NewsFragment extends Fragment{
private Button mybutton;
private TextView textview;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.news,container,false);
textview = (TextView) getActivity().findViewById(R.id.head_themes);
mybutton = (Button) getActivity().findViewById(R.id.mybutton);
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textview.setText("按下了按钮了");
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
}
后来怀疑是不是应为getActivity()在onCreateView中不能使用,或者不能在onCreateView中设置监听器类,后来将监听器都放到了onActivityCreated()中,但是还是不行,最后是放弃使用了getActivity(),该用getView()后来就行了。
下面的代码是对getActivity()和在ragment生命周期之间的关系研究:
public class NewsFragment extends Fragment{
private TextView textview;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.news,container,false);
textview = (TextView) getActivity().findViewById(R.id.head_themes);
if(getActivity() == null)
Log.i("onCreateView","activity is null");
else {
Log.i("onCreateView", "activity is not null");
}
if(textview == null)
Log.i("onCreateView","textview is null");
else {
Log.i("onCreateView", "textview is not null");
}
return view;
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
textview = (TextView) getActivity().findViewById(R.id.head_themes);
if(getActivity() == null)
Log.i("onAttach","activity is null");
else {
Log.i("onAttach", "activity is not null");
}
if(textview == null)
Log.i("onAttach","textview is null");
else {
Log.i("onAttach", "textview is not null");
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
textview = (TextView) getActivity().findViewById(R.id.head_themes);
if(getActivity() == null)
Log.i("onActivityCreated","activity is null");
else {
Log.i("onActivityCreated", "activity is not null");
}
if(textview == null)
Log.i("onActivityCreated","textview is null");
else {
Log.i("onActivityCreated", "textview is not null");
}
}
}
11-04 21:30:40.593: I/onAttach(3899): activity is not null
11-04 21:30:40.593: I/onAttach(3899): textview is null
11-04 21:30:40.601: I/onCreateView(3899): activity is not null
11-04 21:30:40.601: I/onCreateView(3899): textview is null
11-04 21:30:40.603: I/onActivityCreated(3899): activity is not null
11-04 21:30:40.603: I/onActivityCreated(3899): textview is not null
然后试了一下将获取元素的的操作放到了onActitityCreated中,也可以。所以猜测应该是在onActivityCreated调用之前(onCreateView在onActivityCreated之前被调用)activity和fragment还没有相关联,所以fragment中的控件对象不能使用activity对象来获取,只能使用fragment对应的view对象来获取。而在onActivityCreated调用的时候,activity才与fragment关联,所以这是才能使用getActivity返回的activity对象使用findViewById。
findViewById的使用参照:http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment-android