Fix NPE in Toolbar - framework edition

am: 3b925c1ab0

Change-Id: I340ddff981e938bf6f9bd3f1392eec03f25f3e5d
This commit is contained in:
Chris Banes
2016-06-21 08:16:28 +00:00
committed by android-build-merger

View File

@@ -161,7 +161,7 @@ public class Toolbar extends ViewGroup {
private int mTitleMarginTop; private int mTitleMarginTop;
private int mTitleMarginBottom; private int mTitleMarginBottom;
private final RtlSpacingHelper mContentInsets = new RtlSpacingHelper(); private RtlSpacingHelper mContentInsets;
private int mContentInsetStartWithNavigation; private int mContentInsetStartWithNavigation;
private int mContentInsetEndWithActions; private int mContentInsetEndWithActions;
@@ -270,6 +270,7 @@ public class Toolbar extends ViewGroup {
final int contentInsetRight = final int contentInsetRight =
a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetRight, 0); a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetRight, 0);
ensureContentInsets();
mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight); mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
if (contentInsetStart != RtlSpacingHelper.UNDEFINED || if (contentInsetStart != RtlSpacingHelper.UNDEFINED ||
@@ -463,13 +464,13 @@ public class Toolbar extends ViewGroup {
*/ */
public void setTitleMarginBottom(int margin) { public void setTitleMarginBottom(int margin) {
mTitleMarginBottom = margin; mTitleMarginBottom = margin;
requestLayout(); requestLayout();
} }
@Override @Override
public void onRtlPropertiesChanged(@ResolvedLayoutDir int layoutDirection) { public void onRtlPropertiesChanged(@ResolvedLayoutDir int layoutDirection) {
super.onRtlPropertiesChanged(layoutDirection); super.onRtlPropertiesChanged(layoutDirection);
ensureContentInsets();
mContentInsets.setDirection(layoutDirection == LAYOUT_DIRECTION_RTL); mContentInsets.setDirection(layoutDirection == LAYOUT_DIRECTION_RTL);
} }
@@ -1092,6 +1093,7 @@ public class Toolbar extends ViewGroup {
* @attr ref android.R.styleable#Toolbar_contentInsetStart * @attr ref android.R.styleable#Toolbar_contentInsetStart
*/ */
public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd) { public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd) {
ensureContentInsets();
mContentInsets.setRelative(contentInsetStart, contentInsetEnd); mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
} }
@@ -1112,7 +1114,7 @@ public class Toolbar extends ViewGroup {
* @attr ref android.R.styleable#Toolbar_contentInsetStart * @attr ref android.R.styleable#Toolbar_contentInsetStart
*/ */
public int getContentInsetStart() { public int getContentInsetStart() {
return mContentInsets.getStart(); return mContentInsets != null ? mContentInsets.getStart() : 0;
} }
/** /**
@@ -1132,7 +1134,7 @@ public class Toolbar extends ViewGroup {
* @attr ref android.R.styleable#Toolbar_contentInsetEnd * @attr ref android.R.styleable#Toolbar_contentInsetEnd
*/ */
public int getContentInsetEnd() { public int getContentInsetEnd() {
return mContentInsets.getEnd(); return mContentInsets != null ? mContentInsets.getEnd() : 0;
} }
/** /**
@@ -1154,6 +1156,7 @@ public class Toolbar extends ViewGroup {
* @attr ref android.R.styleable#Toolbar_contentInsetRight * @attr ref android.R.styleable#Toolbar_contentInsetRight
*/ */
public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) { public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) {
ensureContentInsets();
mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight); mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
} }
@@ -1174,7 +1177,7 @@ public class Toolbar extends ViewGroup {
* @attr ref android.R.styleable#Toolbar_contentInsetLeft * @attr ref android.R.styleable#Toolbar_contentInsetLeft
*/ */
public int getContentInsetLeft() { public int getContentInsetLeft() {
return mContentInsets.getLeft(); return mContentInsets != null ? mContentInsets.getLeft() : 0;
} }
/** /**
@@ -1194,7 +1197,7 @@ public class Toolbar extends ViewGroup {
* @attr ref android.R.styleable#Toolbar_contentInsetRight * @attr ref android.R.styleable#Toolbar_contentInsetRight
*/ */
public int getContentInsetRight() { public int getContentInsetRight() {
return mContentInsets.getRight(); return mContentInsets != null ? mContentInsets.getRight() : 0;
} }
/** /**
@@ -2128,6 +2131,12 @@ public class Toolbar extends ViewGroup {
} }
} }
private void ensureContentInsets() {
if (mContentInsets == null) {
mContentInsets = new RtlSpacingHelper();
}
}
/** /**
* Accessor to enable LayoutLib to get ActionMenuPresenter directly. * Accessor to enable LayoutLib to get ActionMenuPresenter directly.
*/ */