'Android Studio calling fragment function to unhide framelayout

Hey all I am having the worst time trying to get my code top work.

What I am trying to do is call a function in the MainActivity.java from another fragment FragMovies.java that should then unhide the invisible frameLayout which is called videoFrame.

The MainActivity.java has a TabView, ViewPager and the videoFrame.

In my FragMovies.java I have the following code linked to a button:

Button mainButton = (Button) rootView.findViewById(R.id.button0);

mainButton.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        MainActivity MA = new MainActivity();
        MA.VidStart("thelostegg");
    }
});

The onClick event then calls the function VidStart that's on the MainActiviy.java.

The vidStart function looks like this that's inside the MainActivity.java as well as the onCreate function:

public class MainActivity extends AppCompatActivity {
   TabLayout tabLayout;
   ViewPager viewPager;
   FrameLayout frameLayout;
   PagerAdapter pagerAdapter;
   private Object videoPlay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getViews();

        //adapter setup
        pagerAdapter = new com.example.telluridetainment.PagerAdapter(getSupportFragmentManager());

        //attaching fragments to adapter
        pagerAdapter.addFragment(new FragMovies(),"Movies");
        pagerAdapter.addFragment(new FragShows(),"Shows");

        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

        //setting icons
        tabLayout.getTabAt(0).setIcon(R.drawable.ic_movie_white_24dp);
        tabLayout.getTabAt(1).setIcon(R.drawable.ic_movie_white_24dp);
    }

    public void VidStart(String theMP4) {
        videoPlay myfragment = new videoPlay();

        FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
        fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

        frameLayout = findViewById(R.id.videoFrame);
        frameLayout.setVisibility(View.VISIBLE);

        fragmentManager.replace(R.id.videoFrame, myfragment).commit();
}

Everything goes fine until it gets the this line:

frameLayout = findViewById(R.id.videoFrame);

and my app crashes...

If I comment out that part of the code then it crashes on the

fragmentManager.replace(R.id.videoFrame, myfragment).commit();

BUT only caling the function from the other fragment. If I put the function call in the MainActivity.java onCreate function then it loads up the video and I can hear my video playing but of course can not see it due to the videoFrame still not visible.

The MainActivity XML:

enter image description here

The fragMovies XML (that's inside the MainActivity viewPager):

enter image description here

The _fragvideoplay XML (Called from the VideoPlay.java):

enter image description here

Basically I am looking to fix the calling of the function from another fragment (class) and have it work like it does being called within it's own class.

I'm sure I'm going about this all wrong. Any help would be great!



Sources

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

Source: Stack Overflow

Solution Source