Android使用Fragment,不能失去Fragment内部控件,findViewById()结果是Null-已经解决

Android使用Fragment,不能得到Fragment内部控件,findViewById()结果是Null--已经解决

程序非常简单,好长时间没有搞定,郁闷。。。。。。。。。。。。在论坛咨询,终于找到答案。

描述:

一个Activity:MainActivity,内部是一个Fragment:FragmentA,FragmentA里面有TextView。

问题:无论如何也得不到FragmentA内部的TextView,返回值永远是Null

具体描述:

MainActivity的layout:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:name="com.fragmentexercise.FragmentA"
              android:id="@+id/fragment_a"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
</LinearLayout>
FragmentA的layout:fragment_a.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:textSize="18sp"
    android:text="初始化文本" />

MainActivity.java

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

FragmentA.java

public class FragmentA extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_a, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
//		TextView textView1 = (TextView) getView().findViewById(R.id.textView1);
//		textView1.setText("改变");
	}

具体问题:

在FragmentA.onActivityCreated()中,
TextView textView1 = (TextView) getView().findViewById(R.id.textView1); 得到的始终是Null。

在Fragment.onCreateView()中,

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {   第二个参数 container一直是个空值


在MainActivity中,findViewById(R.id.textView1),   同样是空值。


问题在哪里呢??????????


===============================================================================================================

原因


让Activity 继承 android.support.v4.app.FragmentActivity, 让Fragment继承android.support.v4.app.Fragment就可以了

这样,在FragmentA的onStart等方法中,getActivity().findViewById(R.id.textView1) 就能得到这个TextView。
直接继承 android.app.Fragment,就不行。也许是Android的bug吧。

其实我是按照developer.android.com上的trainning的例子做的,那个例子是为了兼容android3.0以前的系统,继承了android.support.v4.app.Fragment,官方例子运行正常。
我的手机是Android4.1的,可以支持Fragment,所以我直接继承了adnroid.app.Fragment,结果就死活不成功。

想不到是这个原因。

1楼lxmxrx昨天 18:08
在onCreateView中nTextView textView1 = (TextView) getActivity().findViewById(R.id.textView1); 或者nView view = inflater.inflate(R.layout.fragment_a, null);nTextView textView1 = (TextView) view.findViewById(R.id.textView1);
Re: intergameover昨天 20:57
回复lxmxrxn谢谢,试了一下,在onCreateView中,nTextView textView1 = (TextView) getActivity().findViewById(R.id.textView1); 这样得不到TextView,我想是onCreateView方法结束后,系统才会把Fragment的layout inflate到Activity上,所以在onCreateView方法内部,通过activity是不能得到控件的。nn第二个方法是可行的,但是有点不明白,为什么View view = inflater.inflate(R.layout.fragment_a, null);第二个参数直接传null,这里不应该用onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)里的container吗?虽然我测试这里的container也是个null。