马会东的博客

马会东的博客

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

说下我的解决方案:

1.确保 appcompat项目的 target版本 低于 实际项目的android版本(就像.net中 低版本的 framewrok项目不能引用高版本framework项目一样)

2.确保 appcompat的项目编码与eclipse工具的编码一致,我就是犯了这个错误,导致引用appcompat总是失败。。。

我的eclipse环境是utf-8编码, 但是 appcompat项目 新建的时候默认是 gbk编码,右键appcompat项目>>属性>>Resource>>把编码改成utf-8即可

3.确保 你的项目和appcompat类库 在同一个工作空间下  workspace, 如果不在一起 很容易报错。。。。。 这是事实。。。。

4.个别项目 不在 同一个 workspace下 也成功了,但是一直没找到为什么,暂时先不提。。。

 

 

 

下面是摘自网络

一、依赖/脱离appcompat

在新版本中Google跟新了一个依赖包,这个包包含了v4和v7的东西(v7是要依赖v4这个包的,所以用到v7时必须用一起的v4),只要你的编译版本compile with是4.0即以上,那么就会默认依赖这个appcompat包,反正你是没办法脱离它了。如果非要脱离他你可以建立一个编译版本是2.3,2.2之类的应用,这样就不会依赖这个包了。

根据实际经验,我们不可能再去建立一个2.x平台的应用,所以我们必须要好好和这个appcompat相处。于是就应该明白下面几点:

1.appcompat删掉后会出错,新建工程后它会自动生成

2.appcompat会影响你的R文件,你取消依赖的时候很可能丢失R文件。解决方式是将xml文件错误删除或者修改,然后才能重新获得R文件

3.appcompat有自己的属性,如果你定义的attribute属性和它重名了,那么就必须要更换名字。这就是android蛋疼的一点,应该注意。

4.appcompat有自己的编译版本,如果它的版本高,你的程序版本低,这样你在依赖的时候会出现找不到资源的问题。所以请保持一致或者比它编译版本高即可。当然你也可以修改它的编译版本。

 

二、supportV4包冲突

程序报错说应用中的v4和appcompat中的v4不一致。这个很常见,因为appcompat中经常更新,所以很容易出现不一致。解决思路是解除依赖关系,删除自己的v4,然后进入构建路径->库->删除私有的库,最后重新添加依赖即可。当然关键点是尽量不要让xml文件中出现错误,这样很难重新生成R文件的。

 

三、appcompat中资源错误

出现这个错误基本上是跟新所致,因为更新了新的资源文件,但你的appcompat编译版本还是旧的,在appcompat属性中提高它的编译版本然后清理下即可解决此问题。但随之而来的是依赖它的工程也会出现这样的问题,也必须提高编译版本。当然你也可以保留之前没升级过的appcompat,具体看情况而定吧。

 

 

 

 


 下面的是开拓思路用的:

 

 

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

 

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

具体使用步骤(针对于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 theappcompat project, browse to <sdk>/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:
    <activity 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.

以上摘自Android官网。

posted on 2015-04-22 22:02  马会东  阅读(8562)  评论(1编辑  收藏  举报