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?
If I understand correctly, I think in your layout file you have to specify parameter: tools:context=".FragmentName"
I noticed several issues, but the main problem why your app is crashing is because of this part:
val fragmentManager: FragmentManager = fragment2.activity!!.getSupportFragmentManager();
val fragmentTransaction = fragmentManager.beginTransaction()
for (activeFragment in fragmentManager.fragments) {
fragmentTransaction.remove(activeFragment)
}
Into fragmentManager
you stored the fragmentManager that belongs to the activity that hosts the fragment2
, in your case the fragment with the layout including the Linearlayout with @+id/principal
id. Then in your for
loop, you remove all the fragments that are managed by that fragmentManager
, which means you removed the fragment2
from it too. So when you later call:
fragmentTransaction.add(id, fragment, tag)
it can't find any view with the id @+id/principal
, because the fragment containing that Linearlayout ( fragment2
) was just removed.
So for your code to work, remove the for
loop and the app should not crash anymore. But the way your code samples look, I assume you want to add FichajesFragment
as part of the fragment2
, in which case you are using wrong fragmentManager
; instead of the activity one, you should use the one that belongs to fragment2
, ie:
val fragmentManager: FragmentManager = fragment2.childFragmentManager
One more note: I got confused by the R.layout.activity_calendario_laboral
, but the layout is actually for the fragment, if I understood it correctly, so just so you know.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.