First of, I've checked this question has been done and have tested the solutions that are explained on this site (although I may have committed errors in doing them as there are so many possible causes for the error).
I mean solutions like the ones offered here:
Android Fragment no view found for ID?
And here:
Android Fragment : No view found for id 0x7f040034
So, I'm explaining my particular case.
First of, I'm setting the layout like this in my fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
root = inflater.inflate(R.layout.activity_calendario_laboral, container, false);
return root;
}
That layout is, summarized, as it follows:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/principal">
[...]
</LinearLayout>
</FrameLayout>
Once the view has been created I get to call this method:
final Fragment thisFragment=this;
KotlinClass.Companion.setCalendarViewdateListener(calendarView,thisFragment,R.id.principal);
In that method of KotlinClass I get to call this function:
setFragment(FichajesFragment(), fragment,"Fichajes",id);
Being the parameter fragment the thisFragment passed to setCalendarViewdateListener function.
I've finally defined setFragment like this:
private fun setFragment(fragment: Fragment, fragment2: Fragment, tag: String, id: Int) {
val fragmentManager: FragmentManager = fragment2.activity!!.getSupportFragmentManager();
val fragmentTransaction = fragmentManager.beginTransaction()
for (activeFragment in fragmentManager.fragments) {
fragmentTransaction.remove(activeFragment)
}
fragmentTransaction.add(id, fragment, tag)
fragmentTransaction.commit()
fragmentManager.executePendingTransactions()
}
It raises an exception when executing fragmentManager.executePendingTransactions() telling that no view found for id, refering to the principal id of the linearlayout.
What am I missing?