在fragment中嵌套使用fragment,如果是在布局文件中使用<fragment>标签进行嵌套,那么在代码文件中必须是用android.support.v4.app.fragment配合android.support.v4.FragmentActivity使用,否则getFragmentManager.findFragmentById会返回空。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/child_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.test.ChildFragment" />
</LinearLayout>
import android.support.v4.app.fragment:
import android.app.view;
public class MyFragment extend Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.f_illegal_local_gather, container, false);
Fragment fragment = getFragmentManager().findFragmentById(R.id.child_fragment);
}