android - What is the recommended design pattern to avoid null pointer exceptions on references within a runnable when calling removeCallbacks() -


referencing code below. myratingbar.setrating , myhandler.postdelayed within runnable sporadically null pointer exception after calling stop(). i'm not sure best way avoid is. problem worsens if runnable contains objects listeners , listeners have references.

private handler myhandler; private runnable myrunnable; private ratingbar myratingbar;  public void start() {     myrunnable = new runnable()     {         public void run()         {             myratingbar.setrating(1);             myhandler.postdelayed(this, 1000);         }     }      myhandler = new handler();     myhandler.postdelayed(myrunnable, 0); }  public void stop() {     if(myhandler != null)     {         myhandler.removecallbacksandmessages(null);         myhandler = null;     }      myrunnable = null;     myratingbar = null; } 

adding stacktrace. in stacktrace valueanimator onanimationupdate executing within runnable. idea same overall. still executing in runnable when stop() goes set null.

08-07 13:53:21.161: e/androidruntime(20183): fatal exception: main 08-07 13:53:21.161: e/androidruntime(20183): process: com.myprocess.myprocess, pid: 20183 08-07 13:53:21.161: e/androidruntime(20183): java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.ratingbar.setlayoutparams(android.view.viewgroup$layoutparams)' on null object reference 08-07 13:53:21.161: e/androidruntime(20183): @ com.myprocess.myprocess.myclass2$6$2.onanimationupdate(myclass.java:487) 08-07 13:53:21.161: e/androidruntime(20183): @ android.animation.valueanimator.animatevalue(valueanimator.java:1283) 08-07 13:53:21.161: e/androidruntime(20183): @ android.animation.valueanimator.animationframe(valueanimator.java:1207) 08-07 13:53:21.161: e/androidruntime(20183): @ android.animation.valueanimator.doanimationframe(valueanimator.java:1248) 08-07 13:53:21.161: e/androidruntime(20183): @ android.animation.valueanimator$animationhandler.doanimationframe(valueanimator.java:659) 08-07 13:53:21.161: e/androidruntime(20183): @ android.animation.valueanimator$animationhandler.run(valueanimator.java:682) 08-07 13:53:21.161: e/androidruntime(20183): @ android.view.choreographer$callbackrecord.run(choreographer.java:777) 08-07 13:53:21.161: e/androidruntime(20183): @ android.view.choreographer.docallbacks(choreographer.java:590) 08-07 13:53:21.161: e/androidruntime(20183): @ android.view.choreographer.doframe(choreographer.java:559) 08-07 13:53:21.161: e/androidruntime(20183): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:763) 08-07 13:53:21.161: e/androidruntime(20183): @ android.os.handler.handlecallback(handler.java:739) 08-07 13:53:21.161: e/androidruntime(20183): @ android.os.handler.dispatchmessage(handler.java:95) 08-07 13:53:21.161: e/androidruntime(20183): @ android.os.looper.loop(looper.java:145) 08-07 13:53:21.161: e/androidruntime(20183): @ android.app.activitythread.main(activitythread.java:6141) 08-07 13:53:21.161: e/androidruntime(20183): @ java.lang.reflect.method.invoke(native method) 08-07 13:53:21.161: e/androidruntime(20183): @ java.lang.reflect.method.invoke(method.java:372) 08-07 13:53:21.161: e/androidruntime(20183): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399) 08-07 13:53:21.161: e/androidruntime(20183): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1194)

i have imagine there better way. if has better answer i'm doing "cover up" problem, please enlighten me. here's i'm shamefully doing now.

private handler myhandler; private runnable myrunnable; private ratingbar myratingbar;  public void start() {     myrunnable = new runnable()     {         public void run()         {             try             {                 myratingbar.setrating(1);                  if(myhandler != null)                 {                     myhandler.postdelayed(this, 1000);                 }             }             catch(exception e)             {                 // nothing             }         }     }      myhandler = new handler();     myhandler.postdelayed(myrunnable, 0); }  public void stop() {     if(myhandler != null)     {         myhandler.removecallbacksandmessages(null);         myhandler = null;     }      myrunnable = null;     myratingbar = null; } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

wso2esb - How to concatenate JSON array values in WSO2 ESB? -