я могу получить доступ к значениям темы пользовательских цветов в моем приложении с помощью:Как программно получить значение пользовательской темы с помощью Robolectric
private int getTextColorFromTheme(int textAppearanceAttrResId, int state) {
TypedValue typedValue = new TypedValue();
>>>>/* missing TypedValue.data in Robolectric!! */
Resources.Theme theme = mContext.getTheme();
// textAppearanceAttrResId is a reference, so it is required to be retrieved this way.
theme.resolveAttribute(textAppearanceAttrResId, typedValue, true);
int[] textColorAttr = new int[]{android.R.attr.textColor};
int indexOfAttrTextColor = 0;
TypedArray a = mContext.obtainStyledAttributes(typedValue.data, textColorAttr);
ColorStateList csl = a.getColorStateList(indexOfAttrTextColor);
>>>>/* NPE thrown here in Robolectric! */
int color = csl.getColorForState(new int[]{state}, -1);
a.recycle();
return color;
}
Но Robolectric бросает NPE при попытке получить доступ к ColorStateList. Я заметил mContext.getTheme() возвращает тему с TypedArray, данные которого 0.
mContext уже сконфигурировано ранее в настройках():
ActivityController<MyActivity> activityController = Robolectric.buildActivity(MyActivity.class).attach();
MyActivity activity = activityController.get();
activity.setTheme(com.sudocoder.android.theme.R.style.Theme_Mine);
activityController.create().start().resume().visible();
mContext = activity;