Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library
通过分析Snackbar源码分析发现
private Snackbar(ViewGroup parent) {
mTargetParent = parent;
mContext = parent.getContext();
ThemeUtils.checkAppCompatTheme(mContext);
LayoutInflater inflater = LayoutInflater.from(mContext);
mView = (SnackbarLayout) inflater.inflate(
R.layout.design_layout_snackbar, mTargetParent, false);
mAccessibilityManager = (AccessibilityManager)
mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
}
他的构造方法里有一行是ThemeUtils.checkAppCompatTheme(mContext); 一看就是主题检查
class ThemeUtils {
private static final int[] APPCOMPAT_CHECK_ATTRS = { R.attr.colorPrimary };
static void checkAppCompatTheme(Context context) {
TypedArray a = context.obtainStyledAttributes(APPCOMPAT_CHECK_ATTRS);
final boolean failed = !a.hasValue(0);
if (a != null) {
a.recycle();
}
if (failed) {
throw new IllegalArgumentException("You need to use a Theme.AppCompat theme "
+ "(or descendant) with the design library.");
}
}
}
就可以看到”You need to use a Theme.AppCompat theme ” 这个异常抛出信息
所以我们的activity 是要有设置主题的
<activity
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:name="com.dn.boucingmenu6.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
加了android:theme=”@style/Theme.AppCompat.Light.NoActionBar” 就没有再报错了 源码里为甚么要做主题检查呢 这是个问题 。
如果sdk跟com.android.support:appcompat-v7 都升级到25版本好像就没这个毛病