当我的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. 洛谷 P2374 搬运工

    P2374 搬运工 题目背景 陈老师喜欢网购书籍,经常一次购它个百八十本,然后拿来倒卖牟取暴利.(ps:描述要看懂) 题目描述 前些天,高一的新同学来了,他便像往常一样兜售他的书,经过一番口舌,同学们 ...

  2. [DP]【最大全零矩阵】【2015.7.9TEST】E

    E 0.9 seconds, 32 MB " 于是乎,你至少证明了你智商比金天成高.也就说你证明了你不是低智儿童,不错不错. 然而这次, 我貌似也卡住了,你给我打下手吧. 勇敢的少年啊快去创 ...

  3. Android内存泄露分析之StrictMode

    转载请注明地址:http://blog.csdn.NET/yincheng886337/article/details/50524709 StrictMode(严格模式)使用 StrictMode严格 ...

  4. [JSOI2009]计数问题

    一个n*m的方格,初始时每个格子有一个整数权值.接下来每次有2种操作: 改变一个格子的权值: 求一个子矩阵中某种特定权值出现的个数. 输入输出格式 输入格式: 第一行有两个数N,M. 接下来N行,每行 ...

  5. 深入并发AQS二

    AQS须要解决下面几个问题: 1.锁状态,怎样保证并发情况下可以安全的更新? 2.当前线程不能获取锁时,放在哪里? AQS是放在一个队列其中 3.怎样提高效率? AQS的主要职责是当获取不到锁时.将线 ...

  6. Day2:数据运算

    一.算数运算 如: #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan print(10%2) #求模(取模) # 0 ...

  7. JS版微信6.0分享接口用法分析

    本文实例讲述了JS版微信6.0分享接口用法.分享给大家供大家参考,具体如下: 为了净化网络,整顿诱导分享及诱导关注行为,微信于2014年12月30日发布了<微信公众平台关于整顿诱导分享及诱导关注 ...

  8. [Angular] Omit relative path by set up in tsconfig.json

    For example, inside you component you want to import a file from two up directory: import store from ...

  9. oracle高效分页存储过程(百万数据级)

    create or replace procedure Pager( page in number,--数据页数,从1开始 pageSize in number,--每页大小 tableName nv ...

  10. Remove Duplicates from Sorted Array [Python]

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...