1. 程式人生 > >設定Activity的主題為對話方塊時報錯解決方案

設定Activity的主題為對話方塊時報錯解決方案

將Activity設定為對話方塊的方法:

報錯資訊如下: 

09-18 12:50:31.530 22599-22599/com.gb.activityreview E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.gb.activityreview, PID: 22599
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gb.activityreview/com.gb.activityreview.ThirdActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2490)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2550)
        at android.app.ActivityThread.access$1100(ActivityThread.java:154)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5615)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

 主要原因是該Activity繼承自AppCompatActivity,而設定的Theme不是AppCompat的

解決方法一: 修改為直接繼承Activity

解決方法二: 修改Activity的Theme屬性