关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
当我的MainActivity继承自v7包中的ActionBarActivity或者AppCompatActivity时,如果在style.xml文件中指定MainActivity所使用的样式如下:
- <style name="AppTheme" parent="android:Theme.Material.NoActionBar">
- <!-- 5.0开始,可以在Style.xml文件中统一配置App的样式 -->
- <!-- 状态栏的颜色 -->
- <item name="colorPrimary">@color/colorPrimary</item>
- <!-- 一级文本的颜色 -->
- <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
- <!-- 二级文本的颜色 -->
- <item name="colorAccent">@color/colorAccent</item>
- </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,代码如下:
- <resources>
- <!-- Base application theme. -->
- <style name="AppTheme" parent="AppTheme.Base">
- <!-- Customize your theme here. -->
- </style>
- <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
- <item name="colorPrimary">@color/colorPrimary</item>
- <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
- <item name="colorAccent">@color/colorAccent</item>
- <item name="android:windowBackground">@android:color/white</item>
- </style>
- </resources>
2、在res文件中创建values-v21文件夹,然后在此文件夹下创建styles.xml文件,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="AppTheme" parent="AppTheme.Base">
- <item name="android:colorPrimary">@color/colorPrimary</item>
- <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
- <item name="android:colorAccent">@color/colorAccent</item>
- </style>
- </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解决方法的更多相关文章
- 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 ...
- 报错:You need to use a Theme.AppCompat theme (or descendant) with this activity.
学习 Activity 生命周期时希望通过 Dialog 主题测试 onPause() 和 onStop() 的区别,点击按钮跳转 Activity 时报错: E/AndroidRuntime: FA ...
- 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 ...
- You need to use a Theme.AppCompat theme
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dji.sdk.sample/com.dji.sdk.sa ...
- 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 ...
- 关于新版SDK报错You need to use a Theme.AppCompat theme的两种解决办法
android的一个小问题: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme ( ...
- 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 ...
- 开发中遇到的问题(一)——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 ...
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme
配置: 中设置theme为 <application android:allowBackup="true" android:icon="@mipmap/ic_lau ...
随机推荐
- servlet、filter、listener继承的基类和获得作用域的方式
一.servlet: 1.servlet属于j2ee的组件,构建servlet的web project不需要导入项目框架jar包 2.servlet的体系结构: 在j2ee API中,提供给serv ...
- 执行spark-shell时遇到的主机地址的错误
下载了spark 1.4,执行spark-shell时遇到以下错误: java.net.UnknownHostException: UKON-M-Q0EP: UKON-M-Q0EP: nodename ...
- 关于数据库中的JOIN的用法学习
下面是例子分析 表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 a20050114 5 a20050115 表B记录如下: ...
- 洛谷—— P1434 滑雪
https://www.luogu.org/problem/show?pid=1434#sub 题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜 ...
- 2017.1-TOP5 Android开源库
Colorful (Github) Colorful简单实用,通过这个开源库可以通过编码的方式来改变应用的主题,不再需要定义不同的style dependencies { compile 'com.g ...
- Android实践 -- Android蓝牙设置连接
使用Android Bluetooth APIs将设备通过蓝牙连接并通信,设置蓝牙,查找蓝牙设备,配对蓝牙设备 连接并传输数据,以下是Android系统提供的蓝牙相关的类和接口 BluetoothAd ...
- Codeforces #144 (Div. 1) B. Table (组合数学+dp)
题目链接: B.Table 题意: \(n*m\)的矩阵使每个\(n*n\)矩阵里面准确包含\(k\)个点,问你有多少种放法. \((1 ≤ n ≤ 100; n ≤ m ≤ 10^{18}; 0 ≤ ...
- oracle函数大全-字符串处理函数
字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类 ...
- 8、for 、emumrate、range、if
1.for循环用户按照顺序循环可迭代对象中的内容,PS:break.continueli = [11,22,33,44]for item in li: print item 2.enumrate 为可 ...
- 高级Java工程师必备 ----- 深入分析 Java IO (三)
概述 Java IO即Java 输入输出系统.不管我们编写何种应用,都难免和各种输入输出相关的媒介打交道,其实和媒介进行IO的过程是十分复杂的,这要考虑的因素特别多,比如我们要考虑和哪种媒介进行IO( ...