主题材质Android:您需要将Theme.AppCompat主题(或后代)与设计库一起使用

时间:2016-09-22 06:41:24

标签: android

我收到如下错误消息:

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
    at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36)
    at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:192)
    at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:186)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:465)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:172)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:186)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:334)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:345)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:245)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:548)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:533)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$72(RenderTask.java:659)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

对于temp.xml(布局),如:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="490dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>

清单:

 <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

样式:

<style name="AppTheme" parent="android:Theme.Material.Light">
        <!-- theme customizations -->
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

和gradle app:

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "bippo.co.id.bidapplication"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
:
:

我看过You need to use a Theme.AppCompat theme (or descendant) with this activity

  1. NO延伸至ActionBarActivity,但课程延伸至FragmentActivity
  2. 我想使用Theme Material Light
  3. 请分享我应该做什么:)

    @Michael Spitsin: 我尝试了不同的主题foreach活动(在清单上):

    <application
    
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true">
    
            <activity
                android:name=".MainActivity" android:theme="@style/AppTheme">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity android:name=".FormLogin" android:theme="@style/AppTheme" />
    
            <activity android:name=".FormRegister" android:theme="@style/AppTheme" />
    
            <activity android:name=".FormAccountView" android:theme="@style/AppTheme" />
    
            <activity android:name=".FormSearch" android:theme="@style/AppTheme" />
    
            <activity android:name=".FormItemView" android:theme="@style/CoordinatorTheme"/>
    
            <activity android:name=".Temp" android:theme="@style/CoordinatorTheme" />
    
        </application>
    

    和sytles + styles-21:

    <!-- your theme inherits from the material theme -->
        <style name="AppTheme" parent="android:Theme.Material.Light">
            <!-- theme customizations -->
            <item name="android:windowActionBar">false</item>
            <item name="android:windowNoTitle">true</item>
        </style>
    
        <!-- coordinator layout. -->
        <style name="CoordinatorTheme" parent="Theme.AppCompat.Light">
            <!--Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    

    但我仍然收到了msg错误:

    java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
        at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36)
        at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:192)
        at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:186)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:465)
        at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:172)
        at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105)
        at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:186)
        at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:334)
        at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:345)
        at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:245)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324)
        at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
        at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
        at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:548)
        at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:533)
        at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
        at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
        at com.android.tools.idea.rendering.RenderTask.lambda$inflate$72(RenderTask.java:659)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    

3 个答案:

答案 0 :(得分:2)

这是因为在CoordinatorLayout的构造函数中有一行:

ThemeUtils.checkAppCompatTheme(context);

然后你也可以看到你的例外的堆栈跟踪:

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:192)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:186)

因此,如果您想使用CoordinatorLayout,则需要提供AppCompat主题。

<强>更新

如果您仍想使用材质主题和协调器布局,作为折衷方案,您只能为清单中CoordinatorLayout标记中使用android:theme<activity>属性的活动提供AppCompat主题。查看here了解详情。

答案 1 :(得分:0)

将此行放在menifest

中的应用程序标记中
.delivery-services {

  & a:nth-child(3) img  {
    width: 100px;
  }

  & a:last-child img {
    width: 60px;
  }
}

并在你的styles.xml中

  android:theme="@style/AppTheme"

<application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
         android:theme="@style/AppTheme"
        android:supportsRtl="true">

        ...

        </application>

答案 2 :(得分:0)

按@Mike M的建议: 我用了

<style name="AppTheme" parent="Theme.AppCompat.Light">

已解决