Skip to content

Commit 122e994

Browse files
Merge pull request #34 from douglasjunior/issue-22
add extra check to avoid nullpointer in layoutlisteners. #22
2 parents 35c71dd + f5ec4ac commit 122e994

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

library/src/main/java/io/github/douglasjunior/androidSimpleTooltip/SimpleTooltip.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -371,36 +371,35 @@ public boolean onTouch(View v, MotionEvent event) {
371371
private final ViewTreeObserver.OnGlobalLayoutListener mLocationLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
372372
@Override
373373
public void onGlobalLayout() {
374-
if (dismissed) {
375-
SimpleTooltipUtils.removeOnGlobalLayoutListener(mPopupWindow.getContentView(), this);
376-
return;
377-
}
374+
final PopupWindow popup = mPopupWindow;
375+
if (popup == null || dismissed) return;
378376

379377
if (mMaxWidth > 0 && mContentView.getWidth() > mMaxWidth) {
380378
SimpleTooltipUtils.setWidth(mContentView, mMaxWidth);
381-
mPopupWindow.update(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
379+
popup.update(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
382380
return;
383381
}
384382

385-
SimpleTooltipUtils.removeOnGlobalLayoutListener(mPopupWindow.getContentView(), this);
386-
mPopupWindow.getContentView().getViewTreeObserver().addOnGlobalLayoutListener(mArrowLayoutListener);
383+
SimpleTooltipUtils.removeOnGlobalLayoutListener(popup.getContentView(), this);
384+
popup.getContentView().getViewTreeObserver().addOnGlobalLayoutListener(mArrowLayoutListener);
387385
PointF location = calculePopupLocation();
388-
mPopupWindow.setClippingEnabled(true);
389-
mPopupWindow.update((int) location.x, (int) location.y, mPopupWindow.getWidth(), mPopupWindow.getHeight());
390-
mPopupWindow.getContentView().requestLayout();
386+
popup.setClippingEnabled(true);
387+
popup.update((int) location.x, (int) location.y, popup.getWidth(), popup.getHeight());
388+
popup.getContentView().requestLayout();
391389
createOverlay();
392390
}
393391
};
394392

395393
private final ViewTreeObserver.OnGlobalLayoutListener mArrowLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
396394
@Override
397395
public void onGlobalLayout() {
398-
SimpleTooltipUtils.removeOnGlobalLayoutListener(mPopupWindow.getContentView(), this);
399-
if (dismissed)
400-
return;
396+
final PopupWindow popup = mPopupWindow;
397+
if (popup == null || dismissed) return;
398+
399+
SimpleTooltipUtils.removeOnGlobalLayoutListener(popup.getContentView(), this);
401400

402-
mPopupWindow.getContentView().getViewTreeObserver().addOnGlobalLayoutListener(mAnimationLayoutListener);
403-
mPopupWindow.getContentView().getViewTreeObserver().addOnGlobalLayoutListener(mShowLayoutListener);
401+
popup.getContentView().getViewTreeObserver().addOnGlobalLayoutListener(mAnimationLayoutListener);
402+
popup.getContentView().getViewTreeObserver().addOnGlobalLayoutListener(mShowLayoutListener);
404403
if (mShowArrow) {
405404
RectF achorRect = SimpleTooltipUtils.calculeRectOnScreen(mAnchorView);
406405
RectF contentViewRect = SimpleTooltipUtils.calculeRectOnScreen(mContentLayout);
@@ -435,16 +434,17 @@ public void onGlobalLayout() {
435434
SimpleTooltipUtils.setX(mArrowView, (int) x);
436435
SimpleTooltipUtils.setY(mArrowView, (int) y);
437436
}
438-
mPopupWindow.getContentView().requestLayout();
437+
popup.getContentView().requestLayout();
439438
}
440439
};
441440

442441
private final ViewTreeObserver.OnGlobalLayoutListener mShowLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
443442
@Override
444443
public void onGlobalLayout() {
445-
SimpleTooltipUtils.removeOnGlobalLayoutListener(mPopupWindow.getContentView(), this);
446-
if (dismissed)
447-
return;
444+
final PopupWindow popup = mPopupWindow;
445+
if (popup == null || dismissed) return;
446+
447+
SimpleTooltipUtils.removeOnGlobalLayoutListener(popup.getContentView(), this);
448448

449449
if (mOnShowListener != null)
450450
mOnShowListener.onShow(SimpleTooltip.this);
@@ -457,14 +457,14 @@ public void onGlobalLayout() {
457457
private final ViewTreeObserver.OnGlobalLayoutListener mAnimationLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
458458
@Override
459459
public void onGlobalLayout() {
460-
SimpleTooltipUtils.removeOnGlobalLayoutListener(mPopupWindow.getContentView(), this);
461-
if (dismissed)
462-
return;
460+
final PopupWindow popup = mPopupWindow;
461+
if (popup == null || dismissed) return;
463462

464-
if (mAnimated) {
465-
startAnimation();
466-
}
467-
mPopupWindow.getContentView().requestLayout();
463+
SimpleTooltipUtils.removeOnGlobalLayoutListener(popup.getContentView(), this);
464+
465+
if (mAnimated) startAnimation();
466+
467+
popup.getContentView().requestLayout();
468468
}
469469
};
470470

@@ -500,13 +500,10 @@ public void onAnimationEnd(Animator animation) {
500500
private final ViewTreeObserver.OnGlobalLayoutListener mAutoDismissLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
501501
@Override
502502
public void onGlobalLayout() {
503-
if (dismissed) {
504-
SimpleTooltipUtils.removeOnGlobalLayoutListener(mPopupWindow.getContentView(), this);
505-
return;
506-
}
503+
final PopupWindow popup = mPopupWindow;
504+
if (popup == null || dismissed) return;
507505

508-
if (!mRootView.isShown())
509-
dismiss();
506+
if (!mRootView.isShown()) dismiss();
510507
}
511508
};
512509

0 commit comments

Comments
 (0)