Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/An_Times/article/details/88569418

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 的解决办法

直接使用adb发送广播打开一个Dialog的时候发现报错,但是打开应用,然后应用里面打开dialog又不会报错log如下。百度了一下发现有的人是使用AppCompat的包,然后AndroidManifest.xml里面使用的是Theme,将Theme改成AppTheme就ok了。但是我这样并没有用,找了很久还是报错,最后耐心看了下log发现了蹊跷。
报错的这里面显示示我使用的是V7的 dialog

at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:83)

但是我的dialog主题是 <style name="Dialog" parent="android:style/Theme.Dialog">, 所以没有对上,报错了。原来我在使用AlertDialog的时候导入的是 import android.support.v7.app.AlertDialog;,
所以改成 import android.app.AlertDialog; 就好了。

回过头来,报错了,看log,看log,看log !!! 重要的事情说三遍

03-15 10:38:19.381  5806  5806 E AndroidRuntime: FATAL EXCEPTION: main
03-15 10:38:19.381  5806  5806 E AndroidRuntime: Process: com.jmgo.shutdowndemo, PID: 5806
03-15 10:38:19.381  5806  5806 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.jmgo.shutdowndemo.PowerDownBroadcastReceiver: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.app.ActivityThread.-wrap14(ActivityThread.java)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:148)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:5417)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:731)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:83)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.support.v7.app.AlertController.installContent(AlertController.java:226)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:260)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at com.jmgo.shutdowndemo.ShutdownDialog.onCreate(ShutdownDialog.java:117)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.app.Dialog.show(Dialog.java:295)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at com.jmgo.shutdowndemo.ShutdownDialog.show(ShutdownDialog.java:214)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at com.jmgo.shutdowndemo.PowerDownBroadcastReceiver.onReceive(PowerDownBroadcastReceiver.java:20)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
03-15 10:38:19.381  5806  5806 E AndroidRuntime: 	... 8 more
03-15 10:38:19.381  5904  5904 D AndroidRuntime: Shutting down VM

猜你喜欢

转载自blog.csdn.net/An_Times/article/details/88569418