码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-06-10 12:39:43      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:xmlns   track   详细   content   android   null   文本   creat   不能   

程序很easy。好长时间没有搞定。郁闷。。。。

。。。。

在论坛咨询,最终找到答案。

描写叙述:

一个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。结果就死活不成功。

想不到是这个原因。

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

标签:xmlns   track   详细   content   android   null   文本   creat   不能   

原文地址:http://www.cnblogs.com/cxchanpin/p/6978070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!