0

I am using Android Youtube Player as

<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/youtube_player"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:layout_constraintBottom_toTopOf="@id/idRVUsers"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</com.google.android.youtube.player.YouTubePlayerView>

with a recyclerview component on the same layout.

I have initialized youtube player as

static String[] mStrings;

private YouTubePlayerView mYouTubePlayerView;
private YouTubePlayer.OnInitializedListener mOnInitializedListener;
private YouTubePlayer mYouTubePlayer;


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

    youTubePlayerSetup();  //Initialized successfully.

    getDataFromAPI(new VolleyCallback() {
        @Override
        public void onSuccess(JSONArray result){
            //do stuff here
            loadingPB.setVisibility(View.GONE);
            Toast.makeText(getApplicationContext(), "SUCCESS",Toast.LENGTH_LONG).show();
        }
    });


    // creating a new array list.
    userModalArrayList = new ArrayList<>();

    // initializing our views.
    userRV = findViewById(R.id.idRVUsers);
}

When I play some video using its id and does play however it crashes when I switched to Landscape mode and back to portrait using youtube ui fullscreen controls.

Youtube Player Crashes and throws this error: attempt to invoke interface method 'void com.google.android.youtube.player.YouTubePlayer.loadVideo(java.lang.String)' on a null object reference

sakurai
  • 78
  • 7
Asif Hussain
  • 67
  • 11

1 Answers1

0

I have just tried. Added this line android:configChanges="orientation|keyboardHidden|screenSize" in AndroidManifest.xml

<activity
            android:name=".activities.secondscreen"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:configChanges="orientation|keyboardHidden|screenSize"/>

it now handles orientation changes. and no crash problem seems to occur now.

Asif Hussain
  • 67
  • 11