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

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

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.hjschoolhelper210301201_1, PID: 2778 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hjschoolhelper210301201_1/com.example.hjschoolhelper210301201_1.LoginMainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3645) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7872) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:846) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195) at com.example.hjschoolhelper210301201_1.LoginMainActivity.onCreate(LoginMainActivity.java:26) at android.app.Activity.performCreate(Activity.java:8305) at android.app.Activity.performCreate(Activity.java:8284) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3626) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loopOnce(Looper.java:201)  at android.os.Looper.loop(Looper.java:288)  at android.app.ActivityThread.main(ActivityThread.java:7872)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
05-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mrsongs的心情杂货铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值