Error when using any Android Design Support Library Elements

妖精的绣舞 提交于 2019-11-27 03:56:42

In addition to Emmanuel's answer you could be facing the following problem.

It seems like the design library components need a style which is based on an AppCompat Theme. So try to use "Theme.AppCompat.[...]" as a parent in your style.xml.

Example:

<!-- Base application theme. --> <style name="AppTheme" parent="Base.AppTheme">     <!-- Customize your theme here. --> </style>  <style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">     <item name="android:colorPrimary">@color/primary</item>     <item name="android:colorPrimaryDark">@color/primary_dark</item>     <item name="android:colorAccent">@color/accent</item> </style> 

Then add the following to your build.gradle as well:

compile 'com.android.support:appcompat-v7:22.2.0' 

Additionally you should update the following lines in your gradle as well:

classpath 'com.android.tools.build:gradle:1.2.3' compileSdkVersion 22 buildToolsVersion '22.0.1' targetSdkVersion 22 

Update Support Library on SDK Manager in Extras > Android Support Repository and Android Support Library, that works for me ;)

and dont forget to add the compile 'com.android.support:design:22.2.0' on app.gradle and sync

In my case the same crash while using android.support.design.widget.NavigationView was solved by adding app:itemTextColor attribute to the layout xml:

    <android.support.design.widget.NavigationView         android:id="@+id/navigation"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_gravity="start"         android:fitsSystemWindows="true"         app:headerLayout="@layout/nav_header"         app:itemTextColor="?android:textColorPrimary"         app:menu="@menu/drawer"/> 

Be aware that even if you are using the FloatingActionButton widget that is in the Design Support library, you still have to use srcCompat to reference your icon, if you want the FAB to work on older devices.

<android.support.design.widget.FloatingActionButton     app:srcCompat="@drawable/ic_keyboard_arrow_right_black_24dp"     ... /> 

One thing to double-check is that you have applied your theme correctly in your AndroidManifest.xml file. In my case, I had omitted the android:theme attribute. E.g:

<application ... android:theme="@style/Your.Theme.Here" ... > 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!