java - Null Pointer Exception preparing Audio Player -
i preparing small audio player, , showing list of audio songs device, whenever tap on of song play, everytime getting error.
full logcat:
06-10 15:52:28.440: w/dalvikvm(3732): threadid=1: thread exiting uncaught exception (group=0x41e0d2a0) 06-10 15:52:28.450: e/androidruntime(3732): fatal exception: main 06-10 15:52:28.450: e/androidruntime(3732): java.lang.illegalstateexception: not execute method of activity 06-10 15:52:28.450: e/androidruntime(3732): @ android.view.view$1.onclick(view.java:3699) 06-10 15:52:28.450: e/androidruntime(3732): @ android.view.view.performclick(view.java:4223) 06-10 15:52:28.450: e/androidruntime(3732): @ android.view.view$performclick.run(view.java:17275) 06-10 15:52:28.450: e/androidruntime(3732): @ android.os.handler.handlecallback(handler.java:615) 06-10 15:52:28.450: e/androidruntime(3732): @ android.os.handler.dispatchmessage(handler.java:92) 06-10 15:52:28.450: e/androidruntime(3732): @ android.os.looper.loop(looper.java:137) 06-10 15:52:28.450: e/androidruntime(3732): @ android.app.activitythread.main(activitythread.java:4921) 06-10 15:52:28.450: e/androidruntime(3732): @ java.lang.reflect.method.invokenative(native method) 06-10 15:52:28.450: e/androidruntime(3732): @ java.lang.reflect.method.invoke(method.java:511) 06-10 15:52:28.450: e/androidruntime(3732): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1036) 06-10 15:52:28.450: e/androidruntime(3732): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:803) 06-10 15:52:28.450: e/androidruntime(3732): @ dalvik.system.nativestart.main(native method) 06-10 15:52:28.450: e/androidruntime(3732): caused by: java.lang.reflect.invocationtargetexception 06-10 15:52:28.450: e/androidruntime(3732): @ java.lang.reflect.method.invokenative(native method) 06-10 15:52:28.450: e/androidruntime(3732): @ java.lang.reflect.method.invoke(method.java:511) 06-10 15:52:28.450: e/androidruntime(3732): @ android.view.view$1.onclick(view.java:3694) 06-10 15:52:28.450: e/androidruntime(3732): ... 11 more 06-10 15:52:28.450: e/androidruntime(3732): caused by: java.lang.nullpointerexception 06-10 15:52:28.450: e/androidruntime(3732): @ com.example.syncing.mainactivity.songpicked(mainactivity.java:98) 06-10 15:52:28.450: e/androidruntime(3732): ... 14 more 06-10 15:52:38.065: i/process(3732): sending signal. pid: 3732 sig: 9
check complete source code mainactivity.java:-
public class mainactivity extends activity implements mediaplayercontrol { //song list variables private arraylist<song> songlist; private listview songview; //service private musicservice musicsrv; private intent playintent; //binding private boolean musicbound=false; //controller private musiccontroller controller; //activity , playback pause flags private boolean paused=false, playbackpaused=false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //retrieve list view songview = (listview)findviewbyid(r.id.song_list); //instantiate list songlist = new arraylist<song>(); //get songs device getsonglist(); //sort alphabetically title collections.sort(songlist, new comparator<song>(){ public int compare(song a, song b){ return a.gettitle().compareto(b.gettitle()); } }); //create , set adapter songadapter songadt = new songadapter(this, songlist); songview.setadapter(songadt); //setup controller setcontroller(); } //connect service private serviceconnection musicconnection = new serviceconnection(){ @override public void onserviceconnected(componentname name, ibinder service) { musicbinder binder = (musicbinder)service; //get service musicsrv = binder.getservice(); //pass list musicsrv.setlist(songlist); musicbound = true; } @override public void onservicedisconnected(componentname name) { musicbound = false; } }; //start , bind service when activity starts @override protected void onstart() { super.onstart(); if(playintent==null){ playintent = new intent(this, musicservice.class); bindservice(playintent, musicconnection, context.bind_auto_create); startservice(playintent); } } //user song select public void songpicked(view view){ musicsrv.setsong(integer.parseint(view.gettag().tostring())); musicsrv.playsong(); if(playbackpaused){ setcontroller(); playbackpaused=false; } controller.show(0); } //method retrieve song info device public void getsonglist(){ //query external audio contentresolver musicresolver = getcontentresolver(); uri musicuri = android.provider.mediastore.audio.media.external_content_uri; cursor musiccursor = musicresolver.query(musicuri, null, null, null, null); //iterate on results if valid if(musiccursor!=null && musiccursor.movetofirst()){ //get columns int titlecolumn = musiccursor.getcolumnindex (android.provider.mediastore.audio.media.title); int idcolumn = musiccursor.getcolumnindex (android.provider.mediastore.audio.media._id); int artistcolumn = musiccursor.getcolumnindex (android.provider.mediastore.audio.media.artist); //add songs list { long thisid = musiccursor.getlong(idcolumn); string thistitle = musiccursor.getstring(titlecolumn); string thisartist = musiccursor.getstring(artistcolumn); songlist.add(new song(thisid, thistitle, thisartist)); } while (musiccursor.movetonext()); } } @override public boolean canpause() { return true; } @override public boolean canseekbackward() { return true; } @override public boolean canseekforward() { return true; } @override public int getaudiosessionid() { return 0; } @override public int getbufferpercentage() { return 0; } @override public int getcurrentposition() { if(musicsrv!=null && musicbound && musicsrv.ispng()) return musicsrv.getposn(); else return 0; } @override public int getduration() { if(musicsrv!=null && musicbound && musicsrv.ispng()) return musicsrv.getdur(); else return 0; } @override public boolean isplaying() { if(musicsrv!=null && musicbound) return musicsrv.ispng(); return false; } @override public void pause() { playbackpaused=true; musicsrv.pauseplayer(); } @override public void seekto(int pos) { musicsrv.seek(pos); } @override public void start() { musicsrv.go(); } //set controller private void setcontroller(){ controller = new musiccontroller(this); //set previous , next button listeners controller.setprevnextlisteners(new view.onclicklistener() { @override public void onclick(view v) { playnext(); } }, new view.onclicklistener() { @override public void onclick(view v) { playprev(); } }); //set , show controller.setmediaplayer(this); controller.setanchorview(findviewbyid(r.id.song_list)); controller.setenabled(true); } private void playnext(){ musicsrv.playnext(); if(playbackpaused){ setcontroller(); playbackpaused=false; } controller.show(0); } private void playprev(){ musicsrv.playprev(); if(playbackpaused){ setcontroller(); playbackpaused=false; } controller.show(0); } @override protected void onpause(){ super.onpause(); paused=true; } @override protected void onresume(){ super.onresume(); if(paused){ setcontroller(); paused=false; } } @override protected void onstop() { controller.hide(); super.onstop(); } @override protected void ondestroy() { stopservice(playintent); musicsrv=null; super.ondestroy(); } }
you did not set tag view doesn't have it. getting null on view.gettag().tostring()
or put somewhere settag view or check before calling if null:
if(view.gettag() != null) { ... }
Comments
Post a Comment