You need to use a Theme.AppCompat theme (or descendant) with this activity - 小众知识

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

2014-08-14 16:04:03 苏内容
阅读:4202

最近兼容到2.2甚至以下的版本,使用到了ActionBar,查看android官方解释看起来是个很简单的问题,不过本人走了点弯路,本文总结了一些遇到的问题与大家共勉。 当然了你可以继续使用 Android开源项目ActionBarSherlock,来实现兼容方式,不过既然Android官方有兼容包推出,那最好使用官方的嘛。 首先把你的SDK升级到API-18,确认你的SDK中extras目录V7子目录有appcompat文件夹。

Android官网 具体使用 步骤 (针对于Eclipse):

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 the appcompat  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:
     android:theme="@style/Theme.AppCompat.Light" ... >

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、我直接将android-support-v4.jar android-support-v7-appcompat.jar,放入到我的目标工程不就完事了嘛?!还整一堆步骤那么麻烦,那你就错了,看起来编译没错,实际运行的时候你会越到如下错误:

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) with this activity. 

这个时候你仔细看看,发现原来 继承自 ActionBarActivity的类必须指定固定的集中Theme风格,而这些 Theme 风格是需要导入V7中的 appcompat LIB库工程,编译后再引用才能引用使用。

黑色

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

android:theme="@style/Theme.AppCompat.Light"

2、好的,那我们按照官方的步骤,老老实实的导入工程编译,注意拷贝式导入,并且编译成功。

但是我的应用工程在执行如下步骤后apply:

  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 .
报出如下 android-support-v7-appcompat 库引用错误的问题:

经过一番来回实验以后才发现原来需要将你的应用工程也要放在与 android-support-v7-appcompat放在同一个workspace 中才可以。最后环境搭建成功,你可以尽情使用ActionBarActivity啦。

参考:

http://snailz.diandian.com/post/2014-01-22/40060824622

http://blog.csdn.net/hkg1pek/article/details/9473265

http://blog.csdn.net/appte/article/details/11712591

扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1