I create this post, because i am new at this, and i need a little help. I am doing a little exercise about a application you that put your name, and it returns \"hello (the
You dont need this line: setContentView(txtCambiado);
In my case i just pass the wrong view
to the Method
I encountered this error whenever I omitted a parameter while inflating the view for a fragment in the onCreateView()
method like so:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_reject, container);
return view;
}
The solution is to change the view inflation line to:
View view=inflater.inflate(R.layout.fragment_reject, container,false);
The explanation can be found at the Android guide for fragments
Quoting from the guide, the final parameter in the view initialization statement is false because:
"the system is already inserting the inflated layout into the container—passing true would create a redundant view group in the final layout"
Try this way,hope this will help you to solve your problem.
TextView textView = new TextView(this);
textView.setText("CustomTextView");
addContentView(textView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
I’ve found a solution. I had to clear fragment’s parent from views before destroying and I’ve used the next peace of code for this:
in Java
public void onDestroyView() {
if (rootView != null){
ViewGroup viewGroup = (ViewGroup)rootView.getParent();
if (viewGroup != null){
viewGroup.removeAllViews();
}
}
super.onDestroyView();
}
in Kotlin
override fun onDestroyView() {
if (rootView != null) {
val viewGroup = rootView.parent as ViewGroup?
viewGroup?.removeAllViews();
}
super.onDestroyView()
}
in ActivitySaludo
, this line,
setContentView(txtCambiado);
you must set the content view for the activity only once.