Android Studio运行报错:You need to use a Theme.AppCompat theme (or descendant) with this activity. 的解决办法
程序员文章站
2022-05-27 08:13:46
#1、错误描述
今天,想实现activity不显示标题栏的效果,在项目的androidmanifest.xml文件,对相应的activity添加属性
android:theme="@...
#1、错误描述
今天,想实现activity不显示标题栏的效果,在项目的androidmanifest.xml文件,对相应的activity添加属性
android:theme="@android:style/theme.notitlebar"
具体代码如下:
结果运行app的时候,报了如下错误:
java.lang.illegalstateexception: you need to use a theme.appcompat theme (or descendant) with this activity.
具体错误log如下:
01-01 01:33:41.950 6637-6637/com.xtc.kuwo e/androidruntime: fatal exception: main process: com.xtc.kuwo, pid: 6637 java.lang.runtimeexception: unable to start activity componentinfo{com.xtc.kuwo/com.xtc.kuwo.module.view.activity.kuwomusicplayactivity}: java.lang.illegalstateexception: you need to use a theme.appcompat theme (or descendant) with this activity. at android.app.activitythread.performlaunchactivity(activitythread.java:2325) at android.app.activitythread.handlelaunchactivity(activitythread.java:2387) at android.app.activitythread.access$800(activitythread.java:151) at android.app.activitythread$h.handlemessage(activitythread.java:1303) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5254) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:955) at com.android.internal.os.zygoteinit.main(zygoteinit.java:750) caused by: java.lang.illegalstateexception: you need to use a theme.appcompat theme (or descendant) with this activity. at android.support.v7.app.appcompatdelegateimplv7.createsubdecor(appcompatdelegateimplv7.java:340) at android.support.v7.app.appcompatdelegateimplv7.ensuresubdecor(appcompatdelegateimplv7.java:309) at android.support.v7.app.appcompatdelegateimplv7.setcontentview(appcompatdelegateimplv7.java:273) at android.support.v7.app.appcompatactivity.setcontentview(appcompatactivity.java:136) at com.xtc.kuwo.module.view.activity.kuwomusicplayactivity.oncreate(kuwomusicplayactivity.java:57) at android.app.activity.performcreate(activity.java:5990) at android.app.instrumentation.callactivityoncreate(instrumentation.java:1106) at android.app.activitythread.performlaunchactivity(activitythread.java:2278) at android.app.activitythread.handlelaunchactivity(activitythread.java:2387) at android.app.activitythread.access$800(activitythread.java:151) at android.app.activitythread$h.handlemessage(activitythread.java:1303) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5254) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:955) at com.android.internal.os.zygoteinit.main(zygoteinit.java:750)
如下图所示:
2、错误分析
原来我的activity继承自 android.support.v7.app.appcompatactivity,而不是 android.app.activity。
3、解决方法
方法1:
创建的activity时,如果不是那么强烈需要继承自appcompatactivity,就直接继承activity。
如将activity继承自appcompatactivity:
public class mainactivity extends actionbaractivity
改成activity继承自activity:
public class mainactivity extends activity
方法2:
还是想继承自appcompatactivity,那么根据提示来使用appcompat的theme,即将androidmanifest.xml文件中关于activity的主题配置改成:
android:theme="@style/theme.appcompat.light.noactionbar"
完整代码如下:
问题解决!