android - YouTube player API integration Error NullPointerException -
i getting error while integrating youtube player in android app. error in on line - youtubeview.initialize(config.developer_key, this);
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void com.google.android.youtube.player.youtubeplayerview.initialize(java.lang.string, com.google.android.youtube.player.youtubeplayer$oninitializedlistener)' on null object reference
my code:
private static final int recovery_dialog_request = 1; private youtubeplayerview youtubeview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_you_tube_test); //requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_main); youtubeview = findviewbyid(r.id.youtube_view); // initializing video player developer key youtubeview.initialize(config.developer_key, this); } @override public void oninitializationsuccess(youtubeplayer.provider provider, youtubeplayer youtubeplayer, boolean b) { if (!b) { // loadvideo() auto play video // use cuevideo() method, if don't want play automatically youtubeplayer.loadvideo(config.youtube_video_code); // hiding player controls youtubeplayer.setplayerstyle(youtubeplayer.playerstyle.chromeless); } } @override public void oninitializationfailure(youtubeplayer.provider provider, youtubeinitializationresult youtubeinitializationresult) { if (youtubeinitializationresult.isuserrecoverableerror()) { youtubeinitializationresult.geterrordialog(this, recovery_dialog_request).show(); } else { toast.maketext(this, youtubeinitializationresult.tostring(), toast.length_long).show(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == recovery_dialog_request) { // retry initialization if user performed recovery action getyoutubeplayerprovider().initialize(config.developer_key, this); } } private youtubeplayer.provider getyoutubeplayerprovider() { return (youtubeplayerview) findviewbyid(r.id.youtube_view); }
get api developer key google api console , and add key in separate class below example.
public class config { private config() {} public static final string developer_key="aizasyawhk2xxxxxxx-xxxxxxxxxxxe"; }
Comments
Post a Comment