我编写了android youtube api。我成功创建了youtube api项目,活动开始时我可以播放youtube视频。现在我只想播放视频。点击按钮。我有nullpointexception这是我的代码
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
static private final String DEVELOPER_KEY = "************";
static private final String VIDEO = "hjHnWz3EyHs";
private Button b1;
private YouTubePlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
b1 = (Button) findViewById(R.id.button1);
youTubeView.initialize(DEVELOPER_KEY, this);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
player.loadVideo(VIDEO, 36000); //error is in this line
}
});
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {
Toast.makeText(this, "Oh no! " + error.toString(), Toast.LENGTH_LONG)
.show();
}
@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
//player.loadVideo(VIDEO, 36000);
}
}什么是错误的?如果有人知道解决方案,请帮帮我感谢
您尚未初始化player
。您只声明了private YouTubePlayer player;
。
你可以在上查看样品
https://developers.google.com/youtube/android/player/sample-applications
您在onInitializationSuccess
方法中缺少player
初始化,应该如下所示:
@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
MainActivity.this.player = player;
//do other stuff like customizing player
}
还请查看此处的示例文件夹