Browse files

Add sanity checks for unbound pagers and empty adapters. Closes #24.

  • Loading branch information...
1 parent a57e3dc commit dc5ef8875790cfcda4ef2c9ee04b2664b008a187 @JakeWharton JakeWharton committed Nov 30, 2011
View
15 library/src/com/viewpagerindicator/CirclePageIndicator.java
@@ -186,6 +186,14 @@ public boolean isSnap() {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
+ if (mViewPager == null) {
+ return;
+ }
+ final int count = mViewPager.getAdapter().getCount();
+ if (count == 0) {
+ return;
+ }
+
int longSize;
int longPaddingBefore;
int longPaddingAfter;
@@ -202,7 +210,6 @@ protected void onDraw(Canvas canvas) {
shortPaddingBefore = getPaddingLeft();
}
- final int count = mViewPager.getAdapter().getCount();
final float threeRadius = mRadius * 3;
final float shortOffset = shortPaddingBefore + mRadius;
float longOffset = longPaddingBefore + mRadius;
@@ -242,7 +249,9 @@ protected void onDraw(Canvas canvas) {
}
public boolean onTouchEvent(android.view.MotionEvent ev) {
- if (mViewPager == null) return false;
+ if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
+ return false;
+ }
final int action = ev.getAction();
@@ -423,7 +432,7 @@ private int measureLong(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
- if (specMode == MeasureSpec.EXACTLY) {
+ if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
//We were told how big to be
result = specSize;
} else {
View
16 library/src/com/viewpagerindicator/TitlePageIndicator.java
@@ -290,14 +290,22 @@ public void setClipPadding(float clipPadding) {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
+ if (mViewPager == null) {
+ return;
+ }
+ final int count = mViewPager.getAdapter().getCount();
+ if (count == 0) {
+ return;
+ }
+
//Calculate views bounds
ArrayList<RectF> bounds = calculateAllBounds(mPaintText);
//Make sure we're on a page that still exists
- if(mCurrentPage >= bounds.size())
+ if (mCurrentPage >= bounds.size()) {
setCurrentItem(bounds.size()-1);
+ }
- final int count = mViewPager.getAdapter().getCount();
final int countMinusOne = count - 1;
final float halfWidth = getWidth() / 2f;
final int left = getLeft();
@@ -431,7 +439,9 @@ protected void onDraw(Canvas canvas) {
}
public boolean onTouchEvent(android.view.MotionEvent ev) {
- if (mViewPager == null) return false;
+ if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
+ return false;
+ }
final int action = ev.getAction();

0 comments on commit dc5ef88

Please sign in to comment.