Android Stopping Runnable onBackPressed() -


i have runnable on interval using handler, controls volley requests:

final handler handler = new handler();  runnable runnable = new runnable() {    @override    public void run() {       //volley request...      appcontroller.getinstance().addtorequestqueue(volleyreq);       handler.postdelayed(this, 5000);    } };  handler.postdelayed(runnable, 5000); 

i need requests stop when users press button , confirm dialog. stops location updates, "checks" user out, , switches checkinactivity. can somehow insert removecallbacks() method @ point or go?

onbackpressed():

//checking out button public void onbackpressed() {    alertdialog.builder alertdialogbuilder = new alertdialog.builder(this);    alertdialogbuilder.settitle("checking out!");   alertdialogbuilder.setmessage("are sure want check out?");   alertdialogbuilder.setpositivebutton("yes", new dialoginterface.onclicklistener() {       @override       public void onclick(dialoginterface dialog, int which) {         //inserting here crashes app after pressing button         handler.removecallbacks(runnable);          //disconnects location updates...         mgoogleapiclient.disconnect();          //fires volley post request...         checkout();          intent intent=new intent(orderlistactivity.this,checkinactivity.class);         startactivity(intent);          orderlistactivity.this.finish();        }   });    alertdialogbuilder.setnegativebutton("no", new dialoginterface.onclicklistener() {       @override       public void onclick(dialoginterface dialog, int which) {         dialog.cancel();       }   });    alertdialog alertdialog = alertdialogbuilder.create();   alertdialog.show();  } 

i've tried firing various places crashes app , give me null pointer exception.

crash log:

08-20 19:41:08.540 2815-2815/com.myapp.workingprojectlocal e/androidruntime: fatal exception: main                                                                                process: com.myapp.workingprojectlocal, pid: 2815                                                                                java.lang.nullpointerexception: attempt invoke virtual method 'void android.os.handler.removecallbacks(java.lang.runnable)' on null object reference                                                                                    @ com.myapp.workingprojectlocal.orderlistactivity$12.onclick(orderlistactivity.java:394)                                                                                    @ android.support.v7.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:161)                                                                                    @ android.os.handler.dispatchmessage(handler.java:102)                                                                                    @ android.os.looper.loop(looper.java:154)                                                                                    @ android.app.activitythread.main(activitythread.java:6119)                                                                                    @ java.lang.reflect.method.invoke(native method)                                                                                    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886)                                                                                    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776) 

it means handler null. this:

if (handler !=null && runnable !=null)    handler.removecallbacks(runnable); 

it should fix problem.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -