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

这几天想给自己的后端程序搞个前端架子,准备用安卓来实现,现学现用,直接京东搜索关于安卓的热门书籍,下个PDF搞起。

销量第一的是《第一行代码 Android 第2版》,不错,销量第一,拿来入门应该足够了,不过这书是基于 Android 7 的,谷歌真是神速,到今天 Android 9 都已经发布了

花了半天直接看到了第二章,虽然是安卓萌新,但也勉强实现了手机上的界面 DEMO,真刺激啊。

先运行一下瞧瞧,先点个弹窗试试。

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

根据我菜鸟般的编程经验,这不过是个小问题,后来知道了,这的确是个小问题,但这个问题也让我整整懵逼了一天。

先贴下报错日志:

01-03 22:38:01.430 11502-11502/com.example.activitylifecycletest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.activitylifecycletest, PID: 11502
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activitylifecycletest/com.example.activitylifecycletest.DialogActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:103)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:555)
        at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:466)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.activitylifecycletest.DialogActivity.onCreate(DialogActivity.java:11)
        at android.app.Activity.performCreate(Activity.java:6285)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:103) 

You need to use a Theme.AppCompat theme (or descendant) with this activity.,瞅见了,这句话很好理解,不就是让我换个 AppCompat theme 然后继续使用嘛,小Easy。

一开始呢,书中的代码是下面这样的:

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.

运行了一下,MMP的,这下直接给我找不到这个组件了,就是坑在这里,后来我在网上搜了很多答案,别人答案是什么样的呢?

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

看见没,前面没有那个android,而我改了几十遍,都多加了个 android,这就导致我一晚上在懵逼与谷歌之中度过,为此我还翻了一堆官方文档。

就因为自己一时眼瞎,浪费了一天时间。

谨以此文,以示警醒。

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

上一篇:关于AndroidStudio混淆配置,简单的整理(转)


下一篇:真香!iOS云真机全新上线!