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

今天编写ActionBar时,出现如下错误:

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


从错误提示中提到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">

这是将ActionBar隐藏,如果不想隐藏,可用第二种方法。

2:让我们的activity继承Activity:

   如果不是那么强烈需要继承自ActionBarActivity,就直接继承Activity!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容