问题
my YouTube Player crashes and I do not know why.
This is my Java Code: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_youtube); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
playerFragment = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_player_fragment);
playerFragment.initialize(YouTubeKey, this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
mPlayer = youTubePlayer;
//Enables automatic control of orientation
mPlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
//Show full screen in landscape mode always
mPlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
//System controls will appear automatically
mPlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
if (!b) {
mPlayer.cueVideo("xxx");
mPlayer.loadVideo("xxx");
}
else
{
mPlayer.play();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
mPlayer = null;
String ausgabe = youTubeInitializationResult.toString();
Log.d("hi", ausgabe);
} }
Problem was solved
回答1:
Right now we need some additional information to definitively answer this question, but it appears that this line is returning null, which suggests that you want to take a look at your resource XML and make sure you're grabbing the fragment properly.
getSupportFragmentManager().findFragmentById(R.id.youtube_player_fragment);
You may benefit from taking a look at this related question:
findFragmentById return null
I suspect that the problem may be that in your resources XML, you're not declaring the fragment using the support library.
Try using:
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
回答2:
I tried a lot of these suggestions but none worked for me. I finally ended up creating a container and then replacing it at runtime with the YouTubeFragment. This worked perfectly fine for me.
Created a containerView in my layout file as, please note its just a part of my layout file
<View android:padding="10dp" android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/gray90"/> <FrameLayout android:padding="20dp" android:id="@+id/youtubeContent" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
Then I created an instance of the youtube fragment as below
youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
I then replaced it using the standard way as below:-
FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.youtubeContent,youTubePlayerFragment).commit();
I hope this helps others
来源:https://stackoverflow.com/questions/44269417/youtube-player-crashes-java-lang-nullpointerexception-null-object-reference