Skip to content

Commit

Permalink
Prevent NPE when selecting state if view not initialised
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Dec 9, 2018
1 parent 3b09a56 commit f9613d9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## `1.2.1` (10/11/10)

- Prevent NPE when selecting state if view not initialised.

## `1.2.0` (10/11/10)

- Allow replacing state directly from a string.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Add the dependency:

```gradle
dependencies {
compile 'com.github.davidmigloz:multi-state-switch:1.2.0'
compile 'com.github.davidmigloz:multi-state-switch:1.2.1'
}
```

Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

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

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class MultiStateSwitch extends View {
private Point stateSize = new Point();
private Point stateRadius = new Point();

private boolean initialized = false;
private int currentStateIndex;
private Point currentStateCenter = new Point();

Expand Down Expand Up @@ -254,6 +255,7 @@ private void createDataStructures(int size) {
states = new ArrayList<>(size);
statesStyles = new SparseArray<>(size);
statesSelectors = new ArrayList<>(size);
statesCenters = new ArrayList<>(size);
}

@Override
Expand Down Expand Up @@ -281,6 +283,7 @@ private void populateView() {
populateStates();
calculateBounds();
determineCenterPositions(false);
initialized = true;
}

/**
Expand Down Expand Up @@ -430,6 +433,10 @@ public void selectState(int index) {
* @param notifyStateListeners if true all the listeners will be notified about the new selected state.
*/
public void selectState(int index, boolean notifyStateListeners) {
if(!initialized) {
currentStateIndex = index;
return;
}
int validIndex;
if (index < 0) {
validIndex = 0;
Expand Down Expand Up @@ -475,7 +482,6 @@ private void calculateBounds() {
currentStateCenter.y = getHeight() / 2;
int size = getNumberStates();
int stateWidth = backgroundBounds.width() / size;
statesCenters = new ArrayList<>(size);
for (int i = 1; i <= size; i++) {
statesCenters.add(new Point(backgroundBounds.left + stateWidth * i - stateWidth / 2, currentStateCenter.y));
}
Expand Down

0 comments on commit f9613d9

Please sign in to comment.