1. 程式人生 > >IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

錯誤描述為:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

起因:
我想在Manifest中設定我的activity全屏,程式碼如下:

      <activity
            android:name=".MainActivity"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:
label="@string/app_name" >

原因:
從錯誤提示中提到Theme.AppCompat theme,這是因為我們的activity一定是繼承了相容包中的類,
比如我這裡就無意中繼承了ActionBarActivity,它來自android.support.v7.app.ActionBarActivity。
所以就要使用與其配合的AppCompat的theme才行。

解決:
1.根據提示來使用AppCompat的theme,如下:

     <activity
            android:name=".MainActivity"
            android:
theme="@style/Theme.AppCompat.Light.NoActionBar" android:label="@string/app_name" >

不過我要設定全屏,但我並沒有找到,所以雖然錯誤不見了,但並沒有達到我的預期。

2.如果不是那麼強烈需要繼承自ActionBarActivity,就直接繼承Activity吧。問題自然搞定!

轉載自:https://blog.csdn.net/lincyang/article/details/42673151