报错处理:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme with this activity
记录一个安卓报错的处理方法:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity !
由于三方的SDK给的示例Activity是继承AppCompatActivity,所以我定义一个Activity也参照继承了这个AppCompatActivity,导致整个程序崩溃,网上看了一篇文章说很大一部分原因是我们项目编译版本、构建工具老旧的原因(都是26,gradle也是3.0.0的),所不能继承这个AppCompatActivity。解决办法就是把它改成继承Activity就可以了,修改完之后程序完美运行:
public class McsCloudActivity extends AppCompatActivity {
......
}
修改完之后:
public class McsCloudActivity extends Activity {
......
}
这个是报错的详细描述:
2020-07-22 14:12:49.932 21819-21819/com.mcc E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mcc, PID: 21819
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mcc/com.mcc.ui.McsCloudActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3168)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3305)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2034)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7071)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:928)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.jsmcc.ui.McsCloudActivity.onCreate(McsCloudActivity.java:35)
at android.app.Activity.performCreate(Activity.java:7254)
at android.app.Activity.performCreate(Activity.java:7245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3305)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2034)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7071)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:928)
总结:
总结一下:
AppcompaActivity相对于Activity的主要的两点变化;
1:主界面带有toolbar的标题栏;
2,theme主题只能用android:theme="@style/AppTheme.XXX" : appTheme主题或者其子类,而不能用android:style。 否则会提示错误: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
参考文章链接:https://blog.csdn.net/wyf2017/article/details/81489562
本文地址:https://blog.csdn.net/luqingshuai_eloong/article/details/107512770
推荐阅读
-
报错处理:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme with this activity
-
Android Studio运行报错:You need to use a Theme.AppCompat theme (or descendant) with this activity. 的解决办法
-
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 with this activity