VideoView working on MainActivity but doesnt work on my Fragment

Below is the code provided, I’m trying to shift this video view into my Home Page (Fragment java class). I’ve seen people using get Methods but I just keep getting errors. The video view works fine on the MainActivity java class.


Main Activity Java Class

public class MainActivity extends AppCompatActivity {

    VideoView video;
    String video_url = "https://www.daddyandco.com.my/Daddy-Co-Bartender-Promo.mp4";
    ProgressDialog pd;

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

        video = (VideoView) findViewById(R.id.video);
        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Buffering...");
        pd.show();
        Uri uri = Uri.parse(video_url);
        video.setVideoURI(uri);
        video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                pd.dismiss();
            }
        });
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(video);
        video.setMediaController(mediaController);
    }
}

Home Fragment Java Class

public class HomeFragment extends Fragment {
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

Answer

If you are using Fragment init View view = inflate.....

Use the below code Its works for you

 public class HomeFragment extends Fragment {

    VideoView video;
    String video_url = "https://www.daddyandco.com.my/Daddy-Co-Bartender-Promo.mp4";
    ProgressDialog pd;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);
        video = view.findViewById(R.id.video);
        pd = new ProgressDialog(view.getContext());
        pd.setMessage("Buffering...");
        pd.show();
        Uri uri = Uri.parse(video_url);
        video.setVideoURI(uri);
        video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                pd.dismiss();
            }
        });
        MediaController mediaController = new MediaController(view.getContext());
        mediaController.setAnchorView(video);
        video.setMediaController(mediaController);
        return view;
    }
}