androidandroid-layoutandroid-fragmentsfragmentmanagerfragmenttransaction

can not resolve method add for Fragment Transaction - Android


I was just upgrading old android project and stuck at below code :

 FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
 transaction.add(R.id.instructional_tutorial_video_youtube_view, youTubePlayerFragment).commit();
 return view;

Here, It giving me a compile time error : can not resolve method add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment)

The Xml file named R.id.instructional_tutorial_video_youtube_view is as below :

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.brian.skyazul.fragment.InstructionTutorialVideoViewFragment">
    <include
        android:id="@+id/video_topbar"
        layout="@layout/topbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/instructional_tutorial_video_youtube_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/video_topbar"
        android:layout_centerInParent="true"
        android:layout_gravity="center" />
</RelativeLayout>

What might causing the issue? Or Is there any alternative to resolve this? thanks.


Solution

  • Instead of using this, I just used :

    implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:11.0.1'
    
    <com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
        android:id="@+id/instructional_tutorial_video_youtube_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/video_topbar"
        android:layout_centerInParent="true"
        android:layout_gravity="center" />
    

    and inside Java :

     YouTubePlayerView youTubePlayerView = findViewById(R.id.instructional_tutorial_video_youtube_view);
        youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onReady(@NonNull YouTubePlayer youTubePlayer) {
                String videoId = youtubeVideoURL;//"S0Q4gqBUs7c";
                youTubePlayer.loadVideo(videoId, 0);
            }
        });
    

    and it worked like a charm!