我的Android进阶之旅------>解决错误:You need to use a Theme.AppCompat theme (or descendant) with this activity.

#1、错误描述

今天,想实现Activity不显示标题栏的效果,在项目的AndroidManifest.xml文件,对相应的Activity添加属性

 android:theme="@android:style/Theme.NoTitleBar"

具体代码如下:

<activity android:name=".module.view.activity.KuwoMusicPlayActivity" android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

结果运行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)?

如下图所示:
我的Android进阶之旅------>解决错误:You need to use a Theme.AppCompat theme (or descendant) with this activity._第1张图片

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"

完整代码如下:

<activity android:name=".module.view.activity.KuwoMusicPlayActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

问题解决!

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng

这里写图片描述

你可能感兴趣的:(我的Android进阶之旅------>解决错误:You need to use a Theme.AppCompat theme (or descendant) with this activity.)