'Cannot set text in Fragment created through FragmentStateAdapter held in viewpager2 (Android Studio Java)

I've been trying for ages just to set the text in a fragment i have being held in a viewpager2 which I create in a view pager adapter that extends Fragment state adapter.

Because of this I cannot set the Id or Tags myself and so was confused about how to get the id of the fragments so that I could target the textview and change the text. While I've found many potential solutions online they are either outdated or I haven't implemented them properly as they haven't worked.

I simply need to change the text in my third fragment, however, I assume the simplest solution is to target and get the fragment which I cannot do without the ID.

My View Pager Adapter

My mainactivity that should write to the third fragment

Fragment myFragment = getSupportFragmentManager().findFragmentByTag("f2");
Fragment myFragment = (Fragment) adapter.instantiateItem(viewPager, viewPager.getCurrentItem());

and similar methods haven't worked for reasons I haven't been able to find out. I only need to target one specific fragment. The error displayed in the second image isn't the cause it is simply a display of my current trail and error to get the line working correctly.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source