I’m tring to add a fragment to an activity in order to avoid declaring the layout xml. I’m doing
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new mFragment()).commit();
}
But it crashes with:
java.lang.IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first.
Is there a short form for avoiding this error or I must recover the content layout before and remove all their views? Thanks
Edit mFragment Code:
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;
}
}
Change:
To: