android - Removing event listener as activity pauses or stops -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i facing problem in removing event listener. using initialising listener inside onstart() in activity after collecting user data, app shows toast user data has been uploaded , call onbackpressed() method.
but notice listener still running after activity paused or stopped, because creating toast called under oncancelled() event.
so tried overriding onpause , onstop methods.
@override public void onpause() { super.onpause(); // remove post value event listener if (mlistener != null) { mreadreference.removeeventlistener(mlistener); } }
but causing error :
java.lang.runtimeexception: unable pause activity {deventree.com.thetimothyinitiative/deventree.com.thetimothyinitiative.attendance}: java.lang.nullpointerexception: listener must not null
i want remove valueeventlistener after data uploaded , before activity calls onbackpressed() method
code activity:
public class attendance extends activity { map<string, boolean> td; arraylist<string> valu; private sparsebooleanarray sbarray; private static final string tag = "attendance"; customadapter myadapter; listview listview; private databasereference mreadreference; private databasereference mpostreference; private valueeventlistener mlistener; private button mbutton; private button btnall; private button btnnone; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_attendance); firebaseuser user = firebaseauth.getinstance().getcurrentuser(); string eid=user.getemail(); system.out.println(eid); //string[] parts = eid.split("@"); string[] parts = eid.split("\\."); eid = parts[0]; system.out.println(eid); date dnow = new date( ); simpledateformat ft = new simpledateformat("e dd-mm-yyyy 'at' hh:mm:ss"); final listview listview = (listview) findviewbyid(r.id.listq); // initialize database mreadreference = firebasedatabase.getinstance().getreference() .child("lists").child(eid); mpostreference = firebasedatabase.getinstance().getreference() .child("users").child(eid).child(ft.format(dnow)).child("attendance record"); //ui elements btnall=(button)findviewbyid(r.id.button40); btnnone=(button)findviewbyid(r.id.button33); mbutton=(button)findviewbyid(r.id.button42); btnall.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { (int = 0; < listview.getcount(); i++) { listview.setitemchecked(i, true); } } }); btnnone.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { int len=listview.getcount(); int lou=listview.getchildcount(); system.out.println(len+" "+lou); ( int i=0; < listview.getcount(); i++) { listview.setitemchecked(i, false); } } }); mbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { system.out.println("hello mf"); sparsebooleanarray checked = listview.getcheckeditempositions(); (int i=0; i<checked.size(); i++) { if (checked.valueat(i)) { string item = listview.getadapter().getitem(checked.keyat(i)).tostring(); td.put(item, true); } else{ string item = listview.getadapter().getitem(checked.keyat(i)).tostring(); td.put(item, false); } } mpostreference.setvalue(td); (map.entry entry : td.entryset()) { system.out.println(entry.getkey() + ", " + entry.getvalue()); } toast.maketext(getapplicationcontext(), "attendance uploaded",toast.length_short).show(); onbackpressed(); } }); } @override public void onstart() { super.onstart(); valueeventlistener mlistener = new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { system.out.println("here"); td = (map<string,boolean>) datasnapshot.getvalue(); (map.entry<string, boolean> entry : td.entryset()) { td.put(entry.getkey(),false); } valu = new arraylist<string>(td.keyset()); arrayadapter<string> dataadapter = new arrayadapter(getapplicationcontext(), r.layout.rowview, valu); listview = (listview) findviewbyid(r.id.listq); listview.setadapter(dataadapter); } @override public void oncancelled(databaseerror databaseerror) { log.w(tag, "loadpost:oncancelled", databaseerror.toexception()); toast.maketext(attendance.this, "failed load list.", toast.length_short).show(); } }; mreadreference.addvalueeventlistener(mlistener); } @override public void onstop() { // remove post value event listener if (mlistener != null) { mreadreference.removeeventlistener(mlistener); system.out.println("doness"); } super.onstop(); } @override public void onpause() { // remove post value event listener if (mlistener != null) { mreadreference.removeeventlistener(mlistener); system.out.println("here"); } super.onpause(); }
}
do before super()
calls. :
@override public void onpause() { // remove post value event listener if (mlistener != null && mreadreference!=null) { mreadreference.removeeventlistener(mlistener); } super.onpause(); }
same on stop()
:
@override public void onstop() { // remove post value event listener if (mlistener != null && mreadreference!=null) { mreadreference.removeeventlistener(mlistener); } super.onstop(); }
edit 1 : remove declaration
onstart()
modifyonstart()
below code :
@override public void onstart() { super.onstart(); // removed valueeventlistener mlistener =new valueeventlistener() { mlistener = new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { system.out.println("here"); td = (map<string,boolean>) datasnapshot.getvalue(); (map.entry<string, boolean> entry : td.entryset()) { td.put(entry.getkey(),false); } valu = new arraylist<string>(td.keyset()); arrayadapter<string> dataadapter = new arrayadapter(getapplicationcontext(), r.layout.rowview, valu); listview = (listview) findviewbyid(r.id.listq); listview.setadapter(dataadapter); } @override public void oncancelled(databaseerror databaseerror) { log.w(tag, "loadpost:oncancelled", databaseerror.toexception()); toast.maketext(attendance.this, "failed load list.", toast.length_short).show(); } }; mreadreference.addvalueeventlistener(mlistener); }
just declare above oncreate()
or above onstart()
.
hope helps :)
Comments
Post a Comment