Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 我的Android進階之旅------)解決錯誤:You need to use a Theme.AppCompat theme (or descendant) with this activity.

我的Android進階之旅------)解決錯誤:You need to use a Theme.AppCompat theme (or descendant) with this activity.

編輯:關於Android編程

#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)?

如下圖所示:
這裡寫圖片描述

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>

問題解決!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved