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

https://blog.csdn.net/jyw935478490/article/details/72868751

按照官网写的第一个APP,出现了You need to use a Theme.AppCompat theme (or descendant) with this activity问题

2018年10月08日 19:10:08 Xia_Leon 阅读数:173
 
版权声明:文章版权归作者所有,转载请务必注明出处。 https://blog.csdn.net/Xia_Leon/article/details/82972154

这是我第一次根据Android官网指导写的APP,链接如下:

Start Another Activity

每一步都严格参照了官网指导,一步一步敲代码、运行,结果报了如下错误:

  1.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
  2.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)
  3.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
  4.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
  5.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
  6.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: at com.huawei.anrdemo.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:13)
  7.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7372)
  8.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
  9.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
  10.  
    10-08 18:08:40.878 18503 18503 E AndroidRuntime: ... 9 more
  11.  
     

本地环境: Android Studio 3.0.1版本,APP 的 Gradle version是4.1,Android Plugin Version是3.0.1,Compile SDK Version是API 26,Min SDK Version是21,Target SDK Version是API 26,其余参数都是默认。

首先,这个问题我没有Google,也没有去百度查,只从其报错上思考了下,意思是说你的这个activity需要使用一个Theme.AppCompat主题或descendant(没有去查这个descendant是什么意思)

既然不懂descendant是什么意思,那姑且就尝试下 Theme.AppCompat 吧

由于官网定义的这个 DisplayMessageActivity 继承的是 AppCompatActivity,然后我就尝试在 AndroidManifest.xml 里面将 DisplayMessageActivity 的定义增加了一个属性 android:theme="@style/Theme.AppCompat",重新运行后果然成功。于是自己得出这个结论:

如果 activity 继承的是 AppCompatActivity,那么它在 AndroidManifest.xml 里面需要设置一个 Theme.AppCompat 主题。

那如果 activity 不继承 AppCompatActivity 呢,然后我又把 DisplayMessageActivity 的定义改成了 extends Activity,并把AndroidManifest里面的 Theme.AppCompat 去掉,同样可以正常运行。

奇怪的是,明明按照官网一步一步敲的代码,为什么官网没有指出这里的问题?

如果知道这个原因的大神,还请留言讨论,谢谢。

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.

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

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

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

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

  10. Android Studio: 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 ...

随机推荐

  1. nvidia 驱动下载地址

    http://www.nvidia.com/Download/index.aspx?lang=en-us

  2. 百度地图point 转化成经纬度

    百度1.0表示的坐标点,直接在1.3的api上使用坐标无法定位,研究了一阵子百度拾取坐标系统的源码才知道,原来1.0的point是Pixel,调用js的转化代码就搞定了 转化方法如下: var b = ...

  3. UVa 10537 The Toll! Revisited (最短路)

    题意:给定一个图,你要从 s 到达 t,当经过大写字母时,要交 ceil(x /20)的税,如果经过小写字母,那么交 1的税,问你到达 t 后还剩下 c 的,那么最少要带多少,并输出一个解,如果多个解 ...

  4. LDA详解

    PART 1 这个性质被叫做共轭性.共轭先验使得后验概率分布的函数形式与先验概率相同,因此使得贝叶斯分析得到了极⼤的简化.   V:文档集中不重复的词汇的数目 语料库共有m篇文档,: 对于文档,由个词 ...

  5. 在VMware中设置CentOS7的网络

    为了能够使用XShell来管理我们安装好的CentOS7系统,所以我们要先设置CentOS7的网络使其能够联网.  1.选择vmware的编辑,然后点击虚拟网络编辑器     2.点击更改设置(需要有 ...

  6. 团体程序设计天梯赛L1-018 大笨钟 2017-03-22 17:29 79人阅读 评论(0) 收藏

    L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称"大笨钟V"的家伙,每 ...

  7. C++ windows下共享内存

    转载:https://blog.csdn.net/tojohnonly/article/details/70246965 共享内存 (也叫内存映射文件) 主要是通过映射机制实现的 , Windows ...

  8. MVC页面移除HTTP Header中服务器信息

    默认情况下,每一个MVC请求的HTTP Header中都会包含着当前服务器的一些信息,出于安全还是性能还是处女座的强迫症等等,都想把这些信息移除掉,增加一些应用程序的神秘感,如下,默认情况下Chrom ...

  9. Python----一些面试题

    1.写出以下结果 print(1<2 and 2==2) print(1<2 and 2==1) print(1>2 and 2==2) 结果: True False False 解 ...

  10. “全栈2019”Java异常第四章:catch代码块作用域详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...