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.

看到这个错误网上搜索了下相关资料解决方案如下

起初是这样


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
修改后如下

package com.example.calculator;


import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MainActivity继承了AppCompatActivity就回出现上述错误,解决的办法就是让MainActivity去继承Activity而不是AppCompatActivity改完之后删掉报错的部分,然后别忘了导入Activity包

总结一下:

AppcompaActivity相对于Activity的主要的两点变化;
1:主界面带有toolbar的标题栏;
2,theme主题只能用android:theme=”@style/AppTheme (appTheme主题或者其子类),而不能用android:style。 否则会提示错误: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
————————————————
版权声明:本文为CSDN博主「流年若逝」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wyf2017/article/details/81489562

posted @ 2022-02-21 11:34  新感觉  阅读(602)  评论(0编辑  收藏  举报