Skip to content

Commit a9ad04b

Browse files
committed
Fix errors in preference layout preview.
1 parent f62c765 commit a9ad04b

File tree

6 files changed

+25
-53
lines changed

6 files changed

+25
-53
lines changed

CHANGELOG.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

core/src/main/java/com/kizitonwose/colorpreference/ColorDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
8282
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
8383
View rootView = layoutInflater.inflate(R.layout.dialog_colors, null);
8484

85-
colorGrid = (GridLayout) rootView.findViewById(R.id.color_grid);
85+
colorGrid = rootView.findViewById(R.id.color_grid);
8686
colorGrid.setColumnCount(numColumns);
8787
repopulateItems();
8888

core/src/main/java/com/kizitonwose/colorpreference/ColorPreference.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private void initAttrs(AttributeSet attrs, int defStyle) {
3636
TypedArray a = getContext().getTheme().obtainStyledAttributes(
3737
attrs, R.styleable.ColorPreference, defStyle, defStyle);
3838

39-
PreviewSize previewSize = PreviewSize.NORMAL;
39+
PreviewSize previewSize;
4040
try {
4141
numColumns = a.getInteger(R.styleable.ColorPreference_numColumns, numColumns);
4242
colorShape = ColorShape.getShape(a.getInteger(R.styleable.ColorPreference_colorShape, 1));
@@ -56,8 +56,10 @@ private void initAttrs(AttributeSet attrs, int defStyle) {
5656
@Override
5757
protected void onBindView(View view) {
5858
super.onBindView(view);
59-
ImageView previewView = (ImageView) view.findViewById(R.id.color_view);
60-
ColorUtils.setColorViewValue(previewView, value, false, colorShape);
59+
ImageView colorView = view.findViewById(R.id.color_view);
60+
if (colorView != null) {
61+
ColorUtils.setColorViewValue(colorView, value, false, colorShape);
62+
}
6163
}
6264

6365
public void setValue(int value) {

core/src/main/java/com/kizitonwose/colorpreference/ColorUtils.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,22 @@ public static void showDialog(Context context, ColorDialog.OnColorSelectedListen
8686
fragment.setOnColorSelectedListener(listener);
8787

8888
Activity activity = Utils.resolveContext(context);
89-
activity.getFragmentManager().beginTransaction()
90-
.add(fragment, tag)
91-
.commit();
89+
if (activity != null) {
90+
activity.getFragmentManager()
91+
.beginTransaction()
92+
.add(fragment, tag)
93+
.commit();
94+
}
9295
}
9396

9497
public static void attach(Context context, ColorDialog.OnColorSelectedListener listener, String tag) {
9598
Activity activity = Utils.resolveContext(context);
96-
ColorDialog fragment = (ColorDialog) activity
97-
.getFragmentManager().findFragmentByTag(tag);
98-
if (fragment != null) {
99-
// re-bind preference to fragment
100-
fragment.setOnColorSelectedListener(listener);
99+
if (activity != null) {
100+
ColorDialog fragment = (ColorDialog) activity.getFragmentManager().findFragmentByTag(tag);
101+
if (fragment != null) {
102+
// re-bind preference to fragment
103+
fragment.setOnColorSelectedListener(listener);
104+
}
101105
}
102106
}
103107

core/src/main/java/com/kizitonwose/colorpreference/Utils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* @author Kizito Nwose
1010
*/
1111

12-
public class Utils {
12+
class Utils {
1313
@Nullable
14-
public static Activity resolveContext(Context context) {
14+
static Activity resolveContext(Context context) {
1515
if (context instanceof Activity) {
1616
return (Activity) context;
1717
} else if (context instanceof ContextWrapper) {

support/src/main/java/com/kizitonwose/colorpreferencecompat/ColorPreferenceCompat.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void initAttrs(AttributeSet attrs, int defStyle) {
4343
TypedArray a = getContext().getTheme().obtainStyledAttributes(
4444
attrs, R.styleable.ColorPreferenceCompat, defStyle, defStyle);
4545

46-
PreviewSize previewSize = PreviewSize.NORMAL;
46+
PreviewSize previewSize;
4747
try {
4848
numColumns = a.getInteger(R.styleable.ColorPreferenceCompat_numColumns, numColumns);
4949
colorShape = ColorShape.getShape(a.getInteger(R.styleable.ColorPreferenceCompat_colorShape, 1));
@@ -63,8 +63,10 @@ private void initAttrs(AttributeSet attrs, int defStyle) {
6363
@Override
6464
public void onBindViewHolder(PreferenceViewHolder holder) {
6565
super.onBindViewHolder(holder);
66-
ImageView previewView = (ImageView) holder.findViewById(R.id.color_view);
67-
ColorUtils.setColorViewValue(previewView, value, false, colorShape);
66+
ImageView colorView = (ImageView) holder.findViewById(R.id.color_view);
67+
if (colorView != null) {
68+
ColorUtils.setColorViewValue(colorView, value, false, colorShape);
69+
}
6870
}
6971

7072
public void setValue(int value) {

0 commit comments

Comments
 (0)