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

  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,代码如下:

  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文件,代码如下:

  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以后的样式可以由我们自己决定

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

  1. 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.exampl ...

  2. 报错:You need to use a Theme.AppCompat theme (or descendant) with this activity.

    学习 Activity 生命周期时希望通过 Dialog 主题测试 onPause() 和 onStop() 的区别,点击按钮跳转 Activity 时报错: E/AndroidRuntime: FA ...

  3. 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 ...

  4. You need to use a Theme.AppCompat theme

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dji.sdk.sample/com.dji.sdk.sa ...

  5. 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 t ...

  6. 关于新版SDK报错You need to use a Theme.AppCompat theme的两种解决办法

    android的一个小问题: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme ( ...

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

    学习<第一行代码>的时候遇到的问题. Process: com.example.sevenun.littledemo, PID: 2085 java.lang.RuntimeExcepti ...

  8. 开发中遇到的问题(一)——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) wit ...

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

    配置: 中设置theme为 <application android:allowBackup="true" android:icon="@mipmap/ic_lau ...

随机推荐

  1. HDU——T 3579 Hello Kiki

    http://acm.hdu.edu.cn/showproblem.php?pid=3579 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  2. C++基于矢量图形库cairo画图图形

    //sudo apt-get install libcairo2-dev //pkg-config --cflags --libs cairo //-I/usr/include/cairo -I/us ...

  3. 不重新启动VMWare虚拟机加入虚拟磁盘的方法(上)

    近期因为业务须要在不重新启动系统的前提下对系统进行扩容,前提是该系统做过lvm.可是没有足够的物理卷(硬盘),所以引出了改文.本文共分为上下两部分.这是第一部分. 文件夹 加入磁盘 做LVM 加入硬盘 ...

  4. 【例题 6-21 UVA - 506】System Dependencies

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 记录每个物品它的依赖有哪些,以及它被哪些东西依赖就可以了. 显式安装的东西不能被隐式删除删掉(就是remove item,然后删除i ...

  5. COGS——C2098. Asm.Def的病毒

    http://www.cogs.pro/cogs/problem/problem.php?pid=2098 ★☆   输入文件:asm_virus.in   输出文件:asm_virus.out    ...

  6. What is corresponding Cron expression to fire in every X seconds, where X > 60? --转载

    原文地址:http://stackoverflow.com/questions/2996280/what-is-corresponding-cron-expression-to-fire-in-eve ...

  7. sql sever 跨库查询

    reconfigure reconfigure select * from openrowset( 'SQLOLEDB', '192.168.1.180'; 'sa'; '123.com',joybl ...

  8. WGS84与WGS84 Web Mercator

    1. WGS84与WGS84 Web Mercator 1.1 关于WGS1984投影坐标系 UTM (Universal Transverse Mercator)坐标系是由美国军方在1947提出的. ...

  9. 2016最新CocoaPods安装与使用

    前言 是不是已经厌烦了将各种库拖拽到Xcode项目中?那么,CocoaPods的出现就帮你解决了这一问题.CocoaPods是Objective-C项目中最有名的类库管理工具,可以解决库与库之间的依赖 ...

  10. AE中Identify查询工具的实现

    原文 AE中Identify查询工具的实现 主要实现点击查询并闪烁显示,并把查询要素的信息通过DataGridView显示出来,主要用到的接口: IIdentity.IArray.IIdentifyO ...