我正在尝试将片段添加到活动中,以避免声明布局 xml。我正在做
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new mFragment()).commit();
}
但它崩溃了:
java.lang.IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first.
是否有避免此错误的简短形式,或者我必须在之前恢复内容布局并删除他们的所有视图?谢谢
编辑 mFragment 代码:
public class mFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View result= inflater.inflate(R.layout.activity_main, container);
return result;
}
}