80,349
社区成员
发帖
与我相关
我的任务
分享
<?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>
<?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="初始化文本" />
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
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("改变");
}
private View view;
private TextView textView1 ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a, container, false);
if (view == null) {
Log.i("FragmentA", " onCreateView:view is null");
} else {
Log.i("FragmentA", "onCreateView:view is NOOOOOOOOOOOOOOOOT null");
}
textView1 = (TextView) view.findViewById(R.id.textView1);
return view;
}
public class FragmentA extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a, container, false);
if (view == null) {
Log.i("FragmentA", " onCreateView:view is null");
} else {
Log.i("FragmentA", "onCreateView:view is NOOOOOOOOOOOOOOOOT null");
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (view == null) {
Log.i("FragmentA", " onActivityCreated:view is null");
} else {
Log.i("FragmentA", "onActivityCreated:view is NOOOOOOOOOOOOOOOOT null");
}
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
if (textView1 == null) {
Log.i("FragmentA", " onActivityCreated:textView1 is null");
} else {
Log.i("FragmentA", "onActivityCreated:textView1 is NOOOOOOOOOOOOOOOOT null");
}
//textView1.setText("改变");
}
}
logcat:
06-04 16:56:26.953: D/ActivityThread(22471): setTargetHeapUtilization:0.25
06-04 16:56:26.953: D/ActivityThread(22471): setTargetHeapIdealFree:8388608
06-04 16:56:26.953: D/ActivityThread(22471): setTargetHeapConcurrentStart:2097152
06-04 16:56:27.314: W/IconCustomizer(22471): can't load transform_config.xml
06-04 16:56:27.344: I/FragmentA(22471): onCreateView:view is NOOOOOOOOOOOOOOOOT null
06-04 16:56:27.344: I/FragmentA(22471): onActivityCreated:view is NOOOOOOOOOOOOOOOOT null
06-04 16:56:27.344: I/FragmentA(22471): onActivityCreated:textView1 is null
06-04 16:56:27.404: I/Adreno200-EGL(22471): <qeglDrvAPI_eglInitialize:294>: EGL 1.4 QUALCOMM build: (CL3090659)
06-04 16:56:27.404: I/Adreno200-EGL(22471): Build Date: 09/03/13 Tue
06-04 16:56:27.404: I/Adreno200-EGL(22471): Local Branch:
06-04 16:56:27.404: I/Adreno200-EGL(22471): Remote Branch:
06-04 16:56:27.404: I/Adreno200-EGL(22471): Local Patches:
06-04 16:56:27.404: I/Adreno200-EGL(22471): Reconstruct Branch:
public class FragmentA extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a, container, false);
return view ;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView textView1 = (TextView) getView().findViewById(R.id.textView1);
textView1.setText("改变");
}