问题描述
我的 YouTube 播放器崩溃了,我不知道为什么.
my YouTube Player crashes and I do not know why.
这是我的 Java 代码:@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_youtube);工具栏工具栏 = (工具栏) findViewById(R.id.toolbar);setSupportActionBar(工具栏);
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);
} }
问题已解决
推荐答案
现在我们需要一些额外的信息来明确回答这个问题,但似乎这一行返回了 null,这表明你想看看您的资源 XML 并确保您正确抓取了片段.
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:
我怀疑问题可能在于在您的资源 XML 中,您没有使用支持库声明片段.
I suspect that the problem may be that in your resources XML, you're not declaring the fragment using the support library.
尝试使用:
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
这篇关于YouTube 播放器崩溃(java.lang.NullPointerException,空对象引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!