关于引用Theme.AppCompat theme的报错问题

建立V7包库(Library)项目以供其他项目引

  1. Chapter: 使用support-v7 ActionBar前的那些坑
    1. 1. 建立V7包库(Library)项目以供其他项目引用
    2. 2. v7库项目themes_base.xml相关文件报错问题
    3. 3. 关于引用Theme.AppCompat theme的报错问题

以前3.0以前的版本要使用ActionBar,必须使用国外大牛写的ActionBarSherlock这个开源项目。2013年的Google I/O大会之后,Google官方在android-support-v7包中添加了ActionBar,开始让2.1以后的版本支持ActionBar,从此以后曾经最火的Android开源项目ActionBarSherlock可以退出历史舞台了。

要是用v7包中ActionBar也很简单,但有一个需要注意的地方。有些人可能刚开始仅仅是把android-support-v7-appcompat.jar导入项目中,但是在设置Activity的theme时会报错,提示找不到"@style/Theme.AppCompat"。这是由于我们要把v7和资源文件一起导入才行,仅仅导入V7包是不够的。

Eclipse下具体使用步骤为(摘自Android官网):

Create a library project based on the support library code:

  1. Make sure you have downloaded the Android Support Library using the SDK Manager.
  2. Create a library project and ensure the required JAR files are included in the project‘s build path:
    1. Select File > Import.
    2. Select Existing Android Code Into Workspace and click Next.
    3. Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding theappcompat project, browse to /extras/android/support/v7/appcompat/.
    4. Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.
    5. In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar andandroid-support-v7-appcompat.jar files to the build path.
    6. Right-click the project and select Build Path > Configure Build Path.
    7. In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
    8. Uncheck Android Dependencies.
    9. Click OK to complete the changes.

You now have a library project for your selected Support Library that you can use with one or more application projects.

Add the library to your application project:

  1. In the Project Explorer, right-click your project and select Properties.
  2. In the Library pane, click Add.
  3. Select the library project and click OK. For example, the appcompat project should be listed as android-support-v7-appcompat.
  4. In the properties window, click OK.

Once your project is set up with the support library, here‘s how to add the action bar:

  1. Create your activity by extending ActionBarActivity.
  2. Use (or extend) one of the Theme.AppCompat themes for your activity. For example:
    
    

Now your activity includes the action bar when running on Android 2.1 (API level 7) or higher.

On API level 11 or higher

The action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or higher. If you don‘t want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar.

简单地说,就是:

  1. 先升级SDK的Support Library;
  2. 以SDK目录下/extras/android/support/v7/appcompat/为基础新建一个项目;
  3. 将这个项目libs下的v4、v7包Build Path;
  4. Configure Build Path。

基于5.0的V7包库项目

然后这个项目就可以被其它项目引用了。

2) v7库项目themes_base.xml相关文件报错问题

如果你用最新的SDK,可能新建的v7库项目会有这样的错误:

错误信息大概类似:

error: Error retrieving parent for item: No resource found that matches the given name ‘android:TextAppearance.Material‘.
error: Error retrieving parent for item: No resource found that matches the given name ‘android:Widget.Material.*‘
error: Error: No resource found that matches the given name: attr ‘android:actionModeShareDrawable‘.

解决办法很简单:

The only thing you have to do is to open the project.properties file of the android-support-v7-appcompat and change the target from target=android-19 to target=android-21. Afterwards just do a Project --> Clean... so that the changes take effect.

就是把编译环境改成SDK 21就行了。两种方法,打开project.properties,将target=android-7(或者其它)改成target=android-21;你也可以右键项目名,点击Android,修改Project Build Target为Google APIs 21。

然后clean一下,项目报错就会消失。

3) 关于引用Theme.AppCompat theme的报错问题

当 v7 库项目搭建好了,并且别的项目也引用了,你觉得应该没什么错误,但是运行报错了,大概报这样的错:

1 java.lang.RuntimeException: Unable to start activity ComponentInfo{jh.com.testandroidlayout/jh.com.testandroidlayout.navigator.NaviViewPagerActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) withthis activity.

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

这错误很明显,也很简单,原来继承自ActionBarActivity的类必须指定固定的集中Theme风格,而这些Theme风格是需要导入V7中的appcompat LIB库工程,编译后再引用才能引用使用。

打开你项目的AndroidManifest.xml,找到android:theme="@style/xxx",改成v7包里提供的方案就行了。比如:

1 android:theme="@style/Theme.AppCompat" 黑色系
2 android:theme="@style/Theme.AppCompat.Light" 浅色系

更多色彩方案可以自己在Application Tab页去Browse。

stackoverflow 上有更详尽的讨论:http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity

时间: 2024-11-03 01:23:29

关于引用Theme.AppCompat theme的报错问题的相关文章

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

报错如下:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test2/com.example.test2.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 解决方法:创建的activity是M

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme

配置: 中设置theme为 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:theme="@android:style/Theme.Light.NoTitleBar" android:label="@string/app_name" > 报错信息: 09-20 10:31:58.185 1392-1

我的Android进阶之旅------&gt;解决错误: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:sty

关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法

当我的MainActivity继承自v7包中的ActionBarActivity或者AppCompatActivity时,如果在style.xml文件中指定MainActivity所使用的样式如下: [java] view plain copy <style name="AppTheme" parent="android:Theme.Material.NoActionBar"> <!-- 5.0开始,可以在Style.xml文件中统一配置App的样

开发中遇到的问题(一)——java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

1.错误描述: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 2.起因: 在Manifest中设置我的activity全屏 1 <activity android:name=".SplashActivity" 2 android:theme="@android:style/Theme.Black.No

Android 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.

java.lang.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 产生原因:Activity继承自AppCompatActivity,在style.xml中使用了不兼容的Theme. 从错误提示中提到Theme.AppCompat theme,这是因为我们的activity一定是继承了兼容包中的类,比如我这里就无意中继承了AppCompa

弹出AlertDialog的时候报You need to use a Theme.AppCompat theme (or descendant) with this activity错误

今天遇到一个bug,用百度地图的时候,我对上面的标注设置了点击监听,设置的相应的反应是弹出一个AlertDialog 记录一解决bug的历程 但是Dialog却没有弹出来,一看AS下面,报了这错,起初我看我gradle.app里面 implementation 'com.android.support:appcompat-v7:24.2.1' 这句话是红色,我以为是包冲突,但是上午我安卓课上我明明也是用的这个,是可以正常弹出Dialog的 然后我查了查网上, 一种说法:很多说把Activity的

在某些情况下明明添加了引用,为何VS还报错&quot;XXX&quot;不存在类型或命名空间(是否缺少程序集引用)

程序主结构:两个程序集DLL,一个OpticalAlarm(主程序),一个OpticalAlarm.Common 问题描述:搭建程序框架时,使用了log4net进行日志处理,在OpticalAlarm.Common编写好后进行生成,成功无错误,在主程序中引用OpticalAlarm.Common.DLL,运行主程序,报错. 解决:猜测是程序使用目标框架引起,log4net的版本所使用的.NET是4.0,查看我的程序版本是.NET 4.0 Client Profile (4.0的精简版)修改为4.