Skip to content

Commit f9613d9

Browse files
committed
Prevent NPE when selecting state if view not initialised
1 parent 3b09a56 commit f9613d9

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## `1.2.1` (10/11/10)
4+
5+
- Prevent NPE when selecting state if view not initialised.
6+
37
## `1.2.0` (10/11/10)
48

59
- Allow replacing state directly from a string.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Add the dependency:
2525

2626
```gradle
2727
dependencies {
28-
compile 'com.github.davidmigloz:multi-state-switch:1.2.0'
28+
compile 'com.github.davidmigloz:multi-state-switch:1.2.1'
2929
}
3030
```
3131

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22

33
ext.versionMajor = 1 // API Changes, adding big new feature, redesign the App
44
ext.versionMinor = 2 // New features in a backwards-compatible manner
5-
ext.versionPatch = 0 // Backwards-compatible bug fixes
5+
ext.versionPatch = 1 // Backwards-compatible bug fixes
66
ext.versionClassifier = null // Pre-releases (alpha, beta, rc, SNAPSHOT...)
77

88
android {

lib/src/main/java/com/davidmiguel/multistateswitch/MultiStateSwitch.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class MultiStateSwitch extends View {
8181
private Point stateSize = new Point();
8282
private Point stateRadius = new Point();
8383

84+
private boolean initialized = false;
8485
private int currentStateIndex;
8586
private Point currentStateCenter = new Point();
8687

@@ -254,6 +255,7 @@ private void createDataStructures(int size) {
254255
states = new ArrayList<>(size);
255256
statesStyles = new SparseArray<>(size);
256257
statesSelectors = new ArrayList<>(size);
258+
statesCenters = new ArrayList<>(size);
257259
}
258260

259261
@Override
@@ -281,6 +283,7 @@ private void populateView() {
281283
populateStates();
282284
calculateBounds();
283285
determineCenterPositions(false);
286+
initialized = true;
284287
}
285288

286289
/**
@@ -430,6 +433,10 @@ public void selectState(int index) {
430433
* @param notifyStateListeners if true all the listeners will be notified about the new selected state.
431434
*/
432435
public void selectState(int index, boolean notifyStateListeners) {
436+
if(!initialized) {
437+
currentStateIndex = index;
438+
return;
439+
}
433440
int validIndex;
434441
if (index < 0) {
435442
validIndex = 0;
@@ -475,7 +482,6 @@ private void calculateBounds() {
475482
currentStateCenter.y = getHeight() / 2;
476483
int size = getNumberStates();
477484
int stateWidth = backgroundBounds.width() / size;
478-
statesCenters = new ArrayList<>(size);
479485
for (int i = 1; i <= size; i++) {
480486
statesCenters.add(new Point(backgroundBounds.left + stateWidth * i - stateWidth / 2, currentStateCenter.y));
481487
}

0 commit comments

Comments
 (0)