[英]Error inflating class android.support.design.widget.TabLayout occurs even after creating a style for TabLayout
I've tried the solution that should make it work(custom style) : 我已经尝试了应该使其工作的解决方案(自定义样式):
folder res\\values-v21\\styles.xml : 文件夹res \\ values-v21 \\ styles.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<!-- inherit from material theme -->
<style name="AppTheme.Base" parent="android:Theme.Material">
<!-- colorPrimary is used for the default action bar background -->
<item name="android:colorPrimary">#3F51B5</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="android:colorPrimaryDark">#303F9F</item>
<!-- colorAccent is used for the default value for colorControlActivated
which is used to tint widgets -->
<item name="android:colorAccent">#FF4081</item>
</style>
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#DD4081</item>
</style>
</resources>
the tab layout in myFragment.xml : myFragment.xml中的选项卡布局:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
*using compile 'com.android.support:design:+'
*使用
compile 'com.android.support:design:+'
backtrace from Crashlytics (Since I've migrated to Android Studio, I can't see the stacktrace there like in Eclipse. Logcat prints some things but not the actual crash excpetion) : 来自Crashlytics的backtrace (因为我已经迁移到Android Studio,所以我无法在其中看到Eclipse中的stacktrace。Logcat可以打印一些内容,但不能显示实际的崩溃排除):
android.view.LayoutInflater.inflate (LayoutInflater.java:539)
android.view.LayoutInflater.inflate (LayoutInflater.java:423)
com.<package>.MyFragment.onCreateView (MyFragment.java:402)
android.app.Fragment.performCreateView (Fragment.java:2220)
android.app.FragmentManagerImpl.moveToState (FragmentManager.java:973)
android.app.FragmentManagerImpl.moveToState (FragmentManager.java:1148)
android.app.BackStackRecord.run (BackStackRecord.java:793)
android.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:1535)
android.app.FragmentManagerImpl$1.run (FragmentManager.java:482)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5417)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)
and then also : 然后:
Caused by java.lang.IllegalArgumentException
android.support.design.widget.ThemeUtils.checkAppCompatTheme (ThemeUtils.java:34)
android.support.design.widget.TabLayout.<init> (TabLayout.java:269)
android.support.design.widget.TabLayout.<init> (TabLayout.java:263)
java.lang.reflect.Constructor.newInstance (Constructor.java)
android.view.LayoutInflater.createView (LayoutInflater.java:619)
android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:764)
android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:704)
android.view.LayoutInflater.rInflate (LayoutInflater.java:835)
android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:798)
android.view.LayoutInflater.rInflate (LayoutInflater.java:838)
android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:798)
android.view.LayoutInflater.rInflate (LayoutInflater.java:838)
android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:798)
android.view.LayoutInflater.inflate (LayoutInflater.java:515)
android.view.LayoutInflater.inflate (LayoutInflater.java:423)
com.<package>.MyFragment.onCreateView (MyFragment.java:402)
android.app.Fragment.performCreateView (Fragment.java:2220)
android.app.FragmentManagerImpl.moveToState (FragmentManager.java:973)
android.app.FragmentManagerImpl.moveToState (FragmentManager.java:1148)
android.app.BackStackRecord.run (BackStackRecord.java:793)
android.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:1535)
android.app.FragmentManagerImpl$1.run (FragmentManager.java:482)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5417)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)
The AppCompat elements (like TabLayout) check they are in an Activity with a Theme.AppCompat theme by checking if the current theme has defined the "colorPrimary" attribute. 通过检查当前主题是否定义了“ colorPrimary”属性,AppCompat元素(如TabLayout)可以检查它们是否在带有Theme.AppCompat主题的Activity中。 You have used the "colorPrimary" attribute from the "android" namespace, which is not the one that AppCompat uses.
您已使用“ android”命名空间中的“ colorPrimary”属性,该属性不是AppCompat使用的属性。 Add
<item name="colorPrimary">#3F51B5</item>
(without the android namespace) to your activity's theme. 将
<item name="colorPrimary">#3F51B5</item>
(没有android名称空间)添加到活动的主题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.