android - NullPointerException when initializing the media player on url -
android - NullPointerException when initializing the media player on url -
i need help, there osvnonoe activiti in history loaded tracks, when click on track opens alert dialog loading page, link redirected, , output link track, send media player in service, nullpointerexception
example link
http://s.spynetstation.com/m/8/loud%20sound/tamfree026/unknown-blitzkrieg-320kbps.mp3
error
fatal exception: main java.lang.nullpointerexception @ android.media.mediaplayer.setdatasource(mediaplayer.java:783) @ android.media.mediaplayer.setdatasource(mediaplayer.java:761) @ com.spynetstation.mediaservice.initt(mediaservice.java:132) @ com.spynetstation.mainactivity$14.shouldoverrideurlloading(mainactivity.java:716) @ android.webkit.callbackproxy.uioverrideurlloading(callbackproxy.java:224) @ android.webkit.callbackproxy.handlemessage(callbackproxy.java:324) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4424) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) @ dalvik.system.nativestart.main(native method)
code activity
public class mainactivity extends fragmentactivity { mediaplayer mediaplayer; static audiomanager am; static checkbox pdastream; button btnnews; static titleadapter titleadapter; static viewpager mviewpager; relativelayout relativelayout1; static string selectstream; public string stream_sel; public static string colors_sel; public int stream; public static int colors; public boolean checker; public static boolean track; public boolean searchtrack; public boolean searchtrackhistory; static sharedpreferences msettings; static textview titlemusic; static boolean replay = false; public musicintentreceiver myreceiver; //history public linearlayout history; public button btn_up; public boolean history_view = false; public static textview history1; public static textview history2; public static textview history3; public static textview history4; public static textview history5; boolean loadingfinished = true; boolean redirect = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); create(); } public void create(){ setcontentview(r.layout.activity_main); //settings msettings = preferencemanager.getdefaultsharedpreferences(this); //language settings lang = msettings.getstring("lang", "default"); if (lang.equals("default")) {lang=getresources().getconfiguration().locale.getcountry();} locale = new locale(lang); locale.setdefault(locale); configuration config = new configuration(); config.locale = locale; log.i("lang change", "locale="+locale); getbasecontext().getresources().updateconfiguration(config, null); //end language mviewpager = (viewpager) findviewbyid(r.id.pager); relativelayout1 = (relativelayout) findviewbyid(r.id.relativelayout1); pdastream = (checkbox) findviewbyid(r.id.pdastream); pdastream.settext(r.string.chkbox); checker = msettings.getboolean("checker", false); log.i("resume: checker=",""+checker); track = msettings.getboolean("track", false); log.i("resume: track=",""+track); searchtrack = msettings.getboolean("searchtrack", false); log.i("resume: searchtrack=",""+searchtrack); searchtrackhistory = msettings.getboolean("searchtrackhistory", false); log.i("resume: searchtrackhistory=",""+searchtrackhistory); stream_sel = msettings.getstring("stream", "1"); log.i("resume: stream_sel=",""+stream_sel); if (stream_sel.equals("0")) { stream = 0;} else if (stream_sel.equals("1")) { stream = 1;} else if (stream_sel.equals("2")) { stream = 2;} else stream = 1; log.i("default stream=", " "+stream); //--- log.i("mainactivity","oncreate"); titleadapter = new titleadapter(getsupportfragmentmanager()); mviewpager.setadapter(titleadapter); mviewpager.setcurrentitem(stream); mviewpager.setoffscreenpagelimit(3); stream_buf = stream; = (audiomanager) getsystemservice(context.audio_service); seekbar music = (seekbar)findviewbyid(r.id.seekbar1); initbar(music, audiomanager.stream_music); if (free) {pdastream.setchecked(true);} else {pdastream.setchecked(checker);} titlemusic = (textview) findviewbyid(r.id.titlemusic); titlemusic.setselected(true); titlemusic.setvisibility(view.visible); callreceiver.state = true; starthistory(); intentfilter filter = new intentfilter(intent.action_headset_plug); myreceiver = new musicintentreceiver(); registerreceiver(myreceiver, filter); searchtrack(); startservice(new intent(this, mediaservice.class)); } public void starthistory() { history1 = (textview) findviewbyid(r.id.history1); history1.setselected(true); history2 = (textview) findviewbyid(r.id.history2); history2.setselected(true); history3 = (textview) findviewbyid(r.id.history3); history3.setselected(true); history4 = (textview) findviewbyid(r.id.history4); history4.setselected(true); history5 = (textview) findviewbyid(r.id.history5); history5.setselected(true); history = (linearlayout) findviewbyid(r.id.history); history.setvisibility(view.invisible); btn_up = (button) findviewbyid(r.id.btn_up); btn_up.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (history_view){ log.i("starthistory","history close"); btn_up.setbackgroundresource(r.drawable.up); history.setvisibility(view.invisible); history_view=false; mediaservice.only_his=false; if (mediaservice.mediaplayer==null){ titlemusic.settext(" "); } } else { log.i("starthistory","history open"); btn_up.setbackgroundresource(r.drawable.up2); history.setvisibility(view.visible); history_view=true; if (mediaservice.mediaplayer==null){ mediaservice.only_his=true; if (stream_buf==0){ mediaservice.inputhistory("http://liquid.spynetstation.com:8000/meta.txt"); mediaservice.searchinhistory("http://liquid.spynetstation.com:8000/linkable_current_track.txt"); } if (stream_buf==1){ mediaservice.inputhistory("http://main.spynetstation.com:8000/meta.txt"); mediaservice.searchinhistory("http://main.spynetstation.com:8000/linkable_current_track.txt"); } if (stream_buf==2){ mediaservice.inputhistory("http://dub.spynetstation.com:8000/meta.txt"); mediaservice.searchinhistory("http://dub.spynetstation.com:8000/linkable_current_track.txt"); } }else {mediaservice.only_his=false;} } } }); } public void searchtrack(){ final string searchurl = "https://www.google.ru/search?q="; titlemusic.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (searchtrack){} else { if (mediaservice.currentlyplaying != null){ startactivity(new intent(intent.action_view, uri.parse(searchurl + mediaservice.currentlyplaying))); } } } }); history1.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (searchtrackhistory){} else { if (mediaservice.his[1] != null){ showtrack(mediaservice.searchhis[0],mediaservice.his[1]); } } } }); history2.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (searchtrackhistory){} else { if (mediaservice.his[2] != null){ showtrack(mediaservice.searchhis[1],mediaservice.his[2]); } } } }); history3.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (searchtrackhistory){} else { if (mediaservice.his[3] != null){ showtrack(mediaservice.searchhis[2],mediaservice.his[3]); } } } }); history4.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (searchtrackhistory){} else { if (mediaservice.his[4] != null){ showtrack(mediaservice.searchhis[3],mediaservice.his[4]); } } } }); history5.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (searchtrackhistory){} else { if (mediaservice.his[5] != null){ showtrack(mediaservice.searchhis[4],mediaservice.his[5]); } } } }); } public void showtrack(final string smetaout,string stitleout){ alertdialog.builder alert = new alertdialog.builder(this); alert.settitle(stitleout); final webview wv = new webview(this); log.d("my webview", "showtrack"); wv.getsettings().setjavascriptenabled(true); wv.loadurl(smetaout); wv.setwebviewclient(new webviewclient() { @override public boolean shouldoverrideurlloading(webview view, string url) { if (!loadingfinished) { redirect = true; } loadingfinished = false; view.loadurl(url); log.d("shouldoverrideurlloading", url); uri myuri = uri.parse(url); mediaservice.initt(myuri); mediaservice.startt(); homecoming true; } @override public void onpagefinished(webview view, string url) { if(!redirect){ loadingfinished = true; } if(loadingfinished && !redirect){ } else{ redirect = false; } } }); alert.setview(wv); alert.setnegativebutton("close", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { dialog.dismiss(); } }); alert.show(); } }
code service
public class mediaservice extends service implements onpreparedlistener,oncompletionlistener{ static boolean isplayingmain = false; static boolean isplayingliquid = false; static boolean isplayingdubstep = false; static mediaplayer mediaplayer; static notificationmanager nm; private static notificationcompat.builder mbuilder; private static notification.builder mnbuilder; public static context ctx; public ibinder onbind(intent paramintent) { homecoming null; } public static void initt(uri urltrack){ seek { mediaplayer = new mediaplayer(); mediaplayer.setaudiostreamtype(audiomanager.stream_music); log.d("initt",""+urltrack); mediaplayer.setdatasource(ctx,urltrack); mediaplayer.prepare(); } grab (ioexception e) { e.printstacktrace(); } grab (illegalargumentexception e) { e.printstacktrace(); } log.i("mediaservice", "prepare"); } public static void startt(){ mediaplayer.setonpreparedlistener(new onpreparedlistener() { @override public void onprepared(mediaplayer mp) { mp.start(); log.i("mediaservice", "start"); } }); } public static void releasemp() { if (mediaplayer != null) { seek { mediaplayer.release(); log.i("mediaservice", "release"); mediaplayer = null; } grab (exception e) { e.printstacktrace(); } } } @override public void oncompletion(mediaplayer mp) { log.i("mediaservice", "oncompletion"); } @override public void onprepared(mediaplayer mp) { log.i("mediaservice", "onprepareed"); } public void oncreate() { super.oncreate(); ctx = getapplicationcontext(); //notif(titlenotif, contentnotif); //this.nm = ((notificationmanager)getsystemservice("notification")); } public void ondestroy() { //this.nm.cancelall(); stopforeground(true); if(callreceiver.telmanager != null) { callreceiver.telmanager.listen(callreceiver.phonelistener, phonestatelistener.listen_none); log.i("callreceiver", "destroy"); } } public int onstartcommand(intent paramintent, int paramint1, int paramint2) { seek { timeunit.seconds.sleep(0); notif(getresources().getstring(r.string.title_notif),getresources().getstring(r.string.title_notif)); homecoming super.onstartcommand(paramintent, paramint1, paramint2); } grab (interruptedexception localinterruptedexception) { (;;) { localinterruptedexception.printstacktrace(); } } } public void notif(string titlenotif, string contentnotif){ //building notification mbuilder = new notificationcompat.builder(ctx) .setsmallicon(r.drawable.spy) .setcontenttitle(titlenotif) .setticker(contentnotif) .setongoing(true) //.addaction(r.drawable.media_play, "play", notificationintent) ; intent notificationintent = new intent(ctx, mainactivity.class); notificationintent.setflags(intent.flag_activity_single_top); pendingintent pendingintent = pendingintent.getactivity(ctx, 0, notificationintent, pendingintent.flag_update_current); mbuilder.setcontentintent(pendingintent); //notification n = mbuilder.build(); //nm.notify(1, n); startforeground(1, mbuilder.build()); } }
thank answers! decided follows:
in service has changed follows:
public static void initt(context context,uri urltrack){ seek { mediaplayer = new mediaplayer(); mediaplayer.setaudiostreamtype(audiomanager.stream_music); log.d("initt",""+urltrack); mediaplayer.setdatasource(context,urltrack); mediaplayer.prepareasync(); } grab (ioexception e) { e.printstacktrace(); }catch (illegalargumentexception e) { e.printstacktrace(); } log.i("mediaservice", "prepare"); }
in activity changed follows:
@override public boolean shouldoverrideurlloading(webview view, string url) { if (!loadingfinished) { redirect = true; } loadingfinished = false; view.loadurl(url); log.d("shouldoverrideurlloading", url); uri myuri = uri.parse(url); context = getapplicationcontext(); mediaservice.releasemp(); mediaservice.initt(context, myuri); mediaservice.startt(); homecoming true; }
android nullpointerexception android-mediaplayer webviewclient
Comments
Post a Comment