From e724ee4b04d121556e9764f72f9b921a0e16392a Mon Sep 17 00:00:00 2001 From: Gilles Debunne Date: Wed, 31 Aug 2011 11:20:27 -0700 Subject: [PATCH] Fix for NPE in Switch Happens when the checked state is set in XML in the CompoundButton constructor. The drawable are not yet loaded at that point. This still work since the Switch constructor calls setChecked, which will invalidate. Change-Id: I76459307e77596ce0f2759e9449eb42287c6bad7 --- core/java/android/widget/Switch.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/java/android/widget/Switch.java b/core/java/android/widget/Switch.java index 4fcb358830dbe..02c9d0353265b 100644 --- a/core/java/android/widget/Switch.java +++ b/core/java/android/widget/Switch.java @@ -161,6 +161,7 @@ public class Switch extends CompoundButton { mMinFlingVelocity = config.getScaledMinimumFlingVelocity(); // Refresh display with current params + refreshDrawableState(); setChecked(isChecked()); } @@ -632,8 +633,9 @@ public class Switch extends CompoundButton { int[] myDrawableState = getDrawableState(); // Set the state of the Drawable - mThumbDrawable.setState(myDrawableState); - mTrackDrawable.setState(myDrawableState); + // Drawable may be null when checked state is set from XML, from super constructor + if (mThumbDrawable != null) mThumbDrawable.setState(myDrawableState); + if (mTrackDrawable != null) mTrackDrawable.setState(myDrawableState); invalidate(); }