Issue
Referencing the code below. myRatingBar.setRating and myHandler.postDelayed within the runnable sporadically get a null pointer exception after calling stop(). I'm not sure what the best way to avoid this is. The problem only worsens if the runnable contains objects with listeners and the 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 this stacktrace a ValueAnimator onAnimationUpdate was executing within the runnable. The idea is the same overall. Something is still executing in the runnable when stop() goes to set it to 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 to invoke virtual method 'void android.widget.RatingBar.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference 08-07 13:53:21.161: E/AndroidRuntime(20183): at com.myprocess.myprocess.MyClass2$6$2.onAnimationUpdate(MyClass.java:487) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.animation.ValueAnimator.animateValue(ValueAnimator.java:1283) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.animation.ValueAnimator.animationFrame(ValueAnimator.java:1207) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1248) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:659) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:682) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.view.Choreographer.doCallbacks(Choreographer.java:590) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.view.Choreographer.doFrame(Choreographer.java:559) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.os.Handler.handleCallback(Handler.java:739) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.os.Handler.dispatchMessage(Handler.java:95) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.os.Looper.loop(Looper.java:145) 08-07 13:53:21.161: E/AndroidRuntime(20183): at android.app.ActivityThread.main(ActivityThread.java:6141) 08-07 13:53:21.161: E/AndroidRuntime(20183): at java.lang.reflect.Method.invoke(Native Method) 08-07 13:53:21.161: E/AndroidRuntime(20183): at java.lang.reflect.Method.invoke(Method.java:372) 08-07 13:53:21.161: E/AndroidRuntime(20183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 08-07 13:53:21.161: E/AndroidRuntime(20183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Solution
I have to imagine there is a better way. If someone has a better answer than what i'm doing to really "cover up" the problem, please enlighten me. Here's what i'm shamefully doing for 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)
{
// do nothing
}
}
}
myHandler = new Handler();
myHandler.postDelayed(myRunnable, 0);
}
public void stop()
{
if(myHandler != null)
{
myHandler.removeCallbacksAndMessages(null);
myHandler = null;
}
myRunnable = null;
myRatingBar = null;
}
Answered By - Priceline Negotiator
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.