关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法

当我的MainActivity继承自v7包中的ActionBarActivity或者AppCompatActivity时,如果在style.xml文件中指定MainActivity所使用的样式如下:

[java] view plain copy

  1. <style name="AppTheme" parent="android:Theme.Material.NoActionBar">
  2. <!-- 5.0开始,可以在Style.xml文件中统一配置App的样式 -->
  3. <!-- 状态栏的颜色 -->
  4. <item name="colorPrimary">@color/colorPrimary</item>
  5. <!-- 一级文本的颜色 -->
  6. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  7. <!-- 二级文本的颜色 -->
  8. <item name="colorAccent">@color/colorAccent</item>
  9. </style>

会报如下错误:

Java.lang.IllegalStateException:You need to use a Theme.AppCompat theme(or descendatn) with this activity

那么如何解决这个问题呢?网上很多人生说将MainActivity改为继承自Activity即可,但是这样的话就早晨无法兼容老版本的样式,或者说是无法再5.0之前的版本实现MaterialDesign的效果,那么该如何正确的修改呢?

解决步骤如下:

1、res/styles.xml文件中重新添加一个style样式AppTheme.Base,然后将AppTheme继承自AppTheme.Base,代码如下:

[html] view plain copy

  1. <resources>
  2. <!-- Base application theme. -->
  3. <style name="AppTheme" parent="AppTheme.Base">
  4. <!-- Customize your theme here. -->
  5. </style>
  6. <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
  7. <item name="colorPrimary">@color/colorPrimary</item>
  8. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  9. <item name="colorAccent">@color/colorAccent</item>
  10. <item name="android:windowBackground">@android:color/white</item>
  11. </style>
  12. </resources>

2、在res文件中创建values-v21文件夹,然后在此文件夹下创建styles.xml文件,代码如下:

[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="AppTheme" parent="AppTheme.Base">
  4. <item name="android:colorPrimary">@color/colorPrimary</item>
  5. <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
  6. <item name="android:colorAccent">@color/colorAccent</item>
  7. </style>
  8. </resources>

说明:values-v21文件夹中的内容是专门针对API21以上的版本所使用的配置文件,也就是说如果是API21之前的文件就是使用res/values中的styles.xml,否则使用values-v21文件夹下的styles.xml

通过以上两步,就可以轻松实现MainActivity还是继承自AppCompatActivity,也就是说可以将Material Design的效果运行在API21之前版本的手机上,并且API21之前的样式和API21以后的样式可以由我们自己决定

时间: 2024-08-15 07:17:53

关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法的相关文章

You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法

报错如下:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test2/com.example.test2.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 解决方法:创建的activity是M

开发中遇到的问题(一)——java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

1.错误描述: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 2.起因: 在Manifest中设置我的activity全屏 1 <activity android:name=".SplashActivity" 2 android:theme="@android:style/Theme.Black.No

关于引用Theme.AppCompat theme的报错问题

建立V7包库(Library)项目以供其他项目引 Chapter: 使用support-v7 ActionBar前的那些坑 1. 建立V7包库(Library)项目以供其他项目引用 2. v7库项目themes_base.xml相关文件报错问题 3. 关于引用Theme.AppCompat theme的报错问题 以前3.0以前的版本要使用ActionBar,必须使用国外大牛写的ActionBarSherlock这个开源项目.2013年的Google I/O大会之后,Google官方在androi

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

错误描述为:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 起因:我想在Manifest中设置我的activity全屏,代码如下: <activity android:name=".MainActivity" android:theme="@android:style/Theme.NoTitleBar.

我的Android进阶之旅------&gt;解决错误:You need to use a Theme.AppCompat theme (or descendant) with this activity.

#1.错误描述 今天,想实现Activity不显示标题栏的效果,在项目的AndroidManifest.xml文件,对相应的Activity添加属性 android:theme="@android:style/Theme.NoTitleBar" 具体代码如下: <activity android:name=".module.view.activity.KuwoMusicPlayActivity" android:theme="@android:sty

java.lang.IllegalStateException: You need to use a theme.appcompat theme (or descendant) with this activity

错误描述:java.lang.IllegalStateException: You need to use a theme.appcompat theme (or descendant) with this activity 产生原因:Activity继承自AppCompatActivity,在style.xml中使用了不兼容的Theme. 从错误提示中提到Theme.AppCompat theme,这是因为我们的activity一定是继承了兼容包中的类,比如我这里就无意中继承了AppCompa

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme

配置: 中设置theme为 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:theme="@android:style/Theme.Light.NoTitleBar" android:label="@string/app_name" > 报错信息: 09-20 10:31:58.185 1392-1

Android studio使用android:style/Theme.Dialog报错:You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)

查找原因是在activity java代码部分继承了compatactivity public class DialogActivity extends AppCompatActivity 但是在AndroidManifest.xml里面确定义成Dialog, <activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog"></activity

you need to use a theme.appcompat theme (or descendant) with this activity 解决办法

当你想隐藏 Androidmanifest.xml <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" > <intent-filter> <action android:name="android.intent.acti