即使在为TabLayout创建样式后出现错误,也会出现类android.support.design.widget.TabLayout

时间:2016-04-29 11:55:00

标签: android 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>

myFragment.xml中的标签布局:

<android.support.design.widget.TabLayout
     android:id="@+id/tabs"
     style="@style/MyCustomTabLayout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

*使用compile 'com.android.support:design:+'

来自Crashlytics的

backtrace (由于我已经迁移到Android Studio,我无法在Eclipse中看到堆栈跟踪.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)

然后也是:

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)

1 个答案:

答案 0 :(得分:2)

AppCompat元素(如TabLayout)通过检查当前主题是否已定义&#34; colorPrimary&#34;来检查它们是否在具有Theme.AppCompat主题的Activity中。属性。你使用了&#34; colorPrimary&#34;来自&#34; android&#34;的属性命名空间,它不是AppCompat使用的命名空间。加 <item name="colorPrimary">#3F51B5</item>(没有安卓名称空间)到您活动的主题。