說下我的解決方案:
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:
- Make sure you have downloaded the Android Support Library using the SDK Manager.
- Create a library project and ensure the required JAR files are included in the project's build path:
- Select File > Import.
- Select Existing Android Code Into Workspace and click Next.
- Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the
appcompat
project, browse to<sdk>/extras/android/support/v7/appcompat/
. - Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.
- 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 theandroid-support-v4.jar
andandroid-support-v7-appcompat.jar
files to the build path. - Right-click the project and select Build Path > Configure Build Path.
- 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, theappcompat
project requires you to export both theandroid-support-v4.jar
andandroid-support-v7-appcompat.jar
files. - Uncheck Android Dependencies.
- 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:
- In the Project Explorer, right-click your project and select Properties.
- In the Library pane, click Add.
- Select the library project and click OK. For example, the
appcompat
project should be listed as android-support-v7-appcompat. - In the properties window, click OK.
Once your project is set up with the support library, here's how to add the action bar:
- Create your activity by extending
ActionBarActivity
. - 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官網。