向Android项目添加支持库

时间:2022-06-03 15:31:48

Why am I having such a hard time getting into the swing of Android development? I've been developing in various languages over the years and, for some reason, just can't seem to get beyond that "jeez-i-still-feel-a-total-noob" stage with Android.

为什么我很难进入Android开发的浪潮中呢?多年来,我一直在用各种语言进行开发,出于某种原因,我似乎无法超越Android的“jeez- I -still feel-a-total-noob”阶段。

So I'm trying to add an Action Bar to my Android 2.3.3 project. No problem, right? The Android developers website has a nice and clear article explaining exactly how to do it. Of course it involves adding the appcompat v7 support library. No problem, even this is documented step-by-step on this page. But, as with just about every such exercise in Android, I find that you can do exactly what the page tells you to do, it still won't work without significant amount of Googling to fix the errors that you encounter afterwards.

我想在我的Android 2。3项目中添加一个动作栏。没问题,对吧?Android开发者网站有一篇清晰的文章,解释了如何去做。当然,它包括添加appcompat v7支持库。没有问题,即使这是一步一步的记录在本页上。但是,就像Android中几乎所有这样的练习一样,我发现你可以完全按照页面的指示去做,如果没有大量的谷歌搜索来修复你之后遇到的错误,它仍然无法工作。

So I've followed the steps under "Using Eclipse" in the section "Adding libraries with resources" in the above link. The first error I get is Unable to resolve target 'android-16'. No problem, this one I could figure out for myself but I'm curious, is there anything in the documents I've been following that would have suggested to me that I need Android 4.1.2 (API16) installed? Did I just read right over it or should I have known by myself that, to do what I'm trying to do, I would need API16?

因此,我在上面的链接“添加具有资源的库”一节中遵循了“使用Eclipse”的步骤。我得到的第一个错误是无法解析目标“android-16”。没问题,这个我自己可以算出来,但是我很好奇,在我一直跟踪的文档中是否有什么东西会建议我安装Android 4.1.2 (API16) ?我是刚刚读到的还是我应该自己知道,做我想做的事,我需要API16?

Never mind, at least I can fix that but then I get a new problem. As soon as I add the android-support-v7-compat library to my project and click the OK button, the console output lights up with errors, the first one being:

没关系,至少我能解决这个问题,但我又遇到了一个新问题。当我将android-support-v7-compat库添加到我的项目并点击OK按钮时,控制台输出会出现错误,第一个是:

C:...\android-support-v7-appcompat\res\values-v14\styles_base.xml:24: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Holo.ActionBar'.

C:\ android-support-v7-appcompat \ res \ values-v14 \ styles_base。错误:为项目检索父元素:没有找到匹配给定名称“android:Widget.Holo.ActionBar”的资源。

and the other 60 odd errors are similar but for different given names.

另外60个奇怪的错误是相似的,但是对于不同的名字。

I would really appreciate if anyone could help me out here. Obviously I'd like to know how to solve this particular problem but if anyone could give me some tips on how to get past this very frustrating stage of learning this new development environment, I would be ever so thankful. What is it that I should have done differently not to run into these kinds of errors, other than following the instructions on the Android Developers website step by step?

如果有人能帮助我,我将不胜感激。显然,我想知道如何解决这个问题,但如果有人能给我一些建议,告诉我如何度过学习这个新的开发环境的这一令人沮丧的阶段,我将非常感激。除了按照Android开发者网站上的指示一步一步地运行之外,我还应该做些什么来避免出现这些错误呢?

9 个解决方案

#1


22  

OK, I guess I should post an answer as I've eventually managed to solve my own problem.

好吧,我想我应该发布一个答案,因为我最终解决了我自己的问题。

It turns out I have to use a build target of Android 4.2.2, regardless of the fact that I'm specifically developing for Android 2.3.3 - I mean, that's why I'm using support libraries after all. I imagine it might be possible that some other lower target (but higher than 2.3.3) would still work I just used the highest one I have installed and it solved the problem.

事实证明,我必须使用Android 4.2.2的构建目标,不管我是专门为Android 2.3.3开发的——我的意思是,这就是为什么我要使用支持库。我想可能还有其他较低的目标(但高于2.3.3)仍然可以工作,我只是使用了我安装的最高的目标,它解决了问题。

How I was supposed to know this from following the step-by-step instructions on the Android Developers website is a mystery to me. The reason I decided to try changing the targetSdkVersion was because of the final section in the page referenced in the question. It reads "If you are increasing the backward compatibility of your existing application to an earlier version of the Android API with the Support Library, make sure to update your application's manifest." Now I'm not increasing the backward compatibility of my existing application. I'm changing an existing application that targets 2.3.3 to be able to include an Action Bar (seems more like "forward compatibility"). I tried upgrading the targekSdkVersion though as I was out of ideas and lo and behold, it worked.

我是如何根据Android开发者网站上的一步一步的说明知道这一点的,这对我来说是一个谜。我之所以决定更改targetSdkVersion,是因为问题中引用的页面的最后一部分。它是这样写的:“如果您正在增加现有应用程序与支持库之前版本的Android API的向后兼容性,请确保更新应用程序的清单。”现在我没有增加现有应用程序的向后兼容性。我正在修改一个以2.3.3为目标的现有应用程序,使其能够包含操作条(看起来更像是“向前兼容”)。我试着升级了targekSdkVersion,虽然我的想法已经过时了,但是,它成功了。

#2


3  

Don't know if it helps you at all, but I've been struggling with a similar issue for several hours and finally managed to resolve it. In my case, inside my own project's styles.xml file, I was referencing the AppCompat style in the wrong way in the parent attribute.

我不知道这对你是否有帮助,但我已经为一个类似的问题挣扎了几个小时,最后终于解决了它。就我而言,在我自己的项目风格中。xml文件,我在父属性中以错误的方式引用AppCompat样式。

I was using:

我用:

<style name="AppBaseTheme" parent="android:Theme.AppCompat">

where I should have used:

我应该使用的地方:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat">

My mistake was in the reference to the theme.

我的错误是涉及到主题。

#3


1  

I found that setting the Android target to 2.3.3 for the library project android-support-v7-appcompat solves this issue. You don't need to set your project's target higher, but the library's target lower.

我发现,将Android目标设置为2.3.3的库项目Android -support-v7-appcompat可以解决这个问题。您不需要将项目的目标设置得更高,但是库的目标要更低。

#4


0  

You have pinpointed the all important document and the relevant section "adding libraries with resources":

你已确定所有重要文件及有关“增加图书馆资源”一节:

http://developer.android.com/tools/support-library/setup.html

http://developer.android.com/tools/support-library/setup.html

The error suggests that your project is not able to find these resources. Please double check the following :

错误提示您的项目无法找到这些资源。请仔细检查以下内容:

  1. You have checked "is library" on the library project

    您已经在库项目中查询了“is library”

  2. You have exported the jars IN THE LIBRARY PROJECT as described in that section

    您已经在库项目中导出了jar,如该部分所述

  3. You have added the library project as a reference to your app project

    您已经添加了库项目作为您的应用程序项目的参考

  4. You have added the android-support-v4.jar to your libs folder in your app project

    您已经添加了android-support-v4。jar到你的libs文件夹在你的应用项目。

If this is all definitely correct, check eclipse for any error messages - is it the library project or the app project that lights up like the proverbial Christmas Tree ?

如果这一切都是绝对正确的,请检查eclipse是否有任何错误消息——是库项目还是像众所周知的圣诞树一样点亮的应用项目?

#5


0  

First of all check if you have done all the described steps as explained on the developers site. Then, for the errors :

首先检查您是否已经完成了开发人员站点上所描述的所有步骤。然后,对于错误:

Unable to resolve target 'android-16'

无法解析目标“android-16”

Make sure that you have installed API16, this errors occurs if you have mentioned your targetsdk to 16 in your Manifest while the sdk for API16 is not installed. You can either install the API or edit the targetApi of your project to the minimum required version, for the support library you are using I think API 11 is the minimum required android version.

确保您已经安装了API16,如果您在清单中提到了targetsdk到16,而API16的sdk没有安装,则会出现此错误。您可以安装API,也可以将项目的targetApi编辑到最小需要的版本,对于您正在使用的支持库,我认为API 11是最小需要的android版本。

android:Widget.Holo.ActionBar

android:Widget.Holo.ActionBar

This error should also be fixed once the targetsdk is fixed in your project. Since to use Holo.ActionBar you'l need to target your project to minimum supported api level ie. 11 or higher.

一旦targetsdk在您的项目中得到修复,这个错误也应该得到修复。因为使用完全。ActionBar你需要将你的项目定位到最小支持的api级ie。11或更高。

#6


0  

I met similar problems. Using API 18, ADT.

我遇到了类似的问题。ADT使用API 18日。

To solve the "Unable to resolve target 'android-16' problem", I remove the import lib project and repeat what the tutorial says.

为了解决“无法解决目标‘android-16’问题”,我删除了import lib项目,并重复本教程中的内容。

Then I found I couldn't really "add" the library to my project. (The instruction "In the Library pane, click Add." there is not that detail. 1. In your project -> properties -> Java build path -> Libraries -> Add Library... -> Android Classpath Container -> Select the lib project 2. In your project -> properties -> Android -> add... -> choose the lib -> apply

然后我发现我不能真正地“添加”这个库到我的项目中。(指令“在库窗格中,单击Add.”,没有那个细节。1。在您的项目中—>属性—> Java构建路径—>库—>添加库…-> Android类路径容器->选择lib项目2。在你的项目中->属性-> Android ->添加…->选择lib ->应用

cheers

干杯

#7


0  

Here is how I avoided the error, " Unable to resolve target 'android-16'", when adding v7.

下面是我如何避免这个错误,“无法在添加v7时解析目标‘android-16’”。

First, I followed the instructions for adding support libraries with resources in eclipse. Following step 4, I clicked Finish. That's when I saw the error message, android-support-v7-appcompat] Unable to resolve target 'android-16'.

首先,我遵循了在eclipse中添加资源支持库的说明。在步骤4之后,我单击Finish。这时我看到了错误信息,android-support-v7-appcompat无法解析目标“android-16”。

Second, I read this post. I also opened the download manager, but did not need anything updated. In order to make sure that I followed the steps exactly as correctly as I could, I deleted the android-support-v7-appcompat project and started over.

第二,我读了这篇文章。我也打开了下载管理器,但是不需要任何更新。为了确保尽可能正确地遵循这些步骤,我删除了android-support-v7-appcompat项目并重新开始。

This second time I didn't check any different boxes, after all. But, I also got no errors at step 4. Who can say why?

这第二次我没有检查任何不同的盒子,毕竟。但是,我在第4步也没有错误。谁能说为什么?

Thanks, Dewald, you expressed in this question the same frustration I have. Is experience the only way out?

谢谢,Dewald,你在这个问题上表达了我同样的沮丧。经验是唯一的出路吗?

#8


0  

If some of your resource xml files cannot find their respective AppCompat references, make sure you also add the support libraries to your project properties. You do this by:

如果您的一些资源xml文件无法找到它们各自的AppCompat引用,请确保您也将支持库添加到项目属性中。你这样做:

1.) right clicking on your project in the project

1.)右键单击项目中的项目

2.) Select properties. (Bottom of the menu)

2)。选择properties。(底部的菜单)

3.) click Android on the left hand side menu of the new pop up window

3)在新弹出窗口的左手边菜单上点击Android

4.) In the libraries section, click the "add button"

4)。在libraries部分,点击“添加按钮”

5.) Select the respective libraries. (If none show up, check to see if they are already displayed in the "libraries" window of the pop up menu. If there is a red x by the desired library, try and resolve that issue first.)

5)。选择相应的库。(如果没有显示,请检查它们是否已经显示在弹出菜单的“libraries”窗口中。如果想要的库中有一个红色的x,请先尝试解决这个问题。

6.) Once there is a green checkmark next to the desired libraries, make sure the "Is Library" option in the libraries section IS NOT selected.

6)。一旦在所需的库旁边有一个绿色的复选标记,请确保在libraries部分中的“is Library”选项没有被选中。

7.) Click apply.

7)。单击apply。

8.) Close the pop up menu.

8)。关闭弹出菜单。

If this doesn't work, let me know and I can help you out. I spent three hours on the problem so I know pretty much all the possible things that can go wrong with the set up.

如果不行,告诉我,我可以帮你。我在这个问题上花了三个小时,所以我几乎知道所有可能出现的问题。

#9


0  

Go to Project -> uncheck Build Automatically

去项目->取消自动构建。

Go to Project -> Clean... , clean both the library project and your app project

去项目->清洁…,清理库项目和应用程序项目

Export your app as a signed APK while Build Automatically is still disabled

将应用程序导出为已签名的APK,而Build仍然是禁用的

#1


22  

OK, I guess I should post an answer as I've eventually managed to solve my own problem.

好吧,我想我应该发布一个答案,因为我最终解决了我自己的问题。

It turns out I have to use a build target of Android 4.2.2, regardless of the fact that I'm specifically developing for Android 2.3.3 - I mean, that's why I'm using support libraries after all. I imagine it might be possible that some other lower target (but higher than 2.3.3) would still work I just used the highest one I have installed and it solved the problem.

事实证明,我必须使用Android 4.2.2的构建目标,不管我是专门为Android 2.3.3开发的——我的意思是,这就是为什么我要使用支持库。我想可能还有其他较低的目标(但高于2.3.3)仍然可以工作,我只是使用了我安装的最高的目标,它解决了问题。

How I was supposed to know this from following the step-by-step instructions on the Android Developers website is a mystery to me. The reason I decided to try changing the targetSdkVersion was because of the final section in the page referenced in the question. It reads "If you are increasing the backward compatibility of your existing application to an earlier version of the Android API with the Support Library, make sure to update your application's manifest." Now I'm not increasing the backward compatibility of my existing application. I'm changing an existing application that targets 2.3.3 to be able to include an Action Bar (seems more like "forward compatibility"). I tried upgrading the targekSdkVersion though as I was out of ideas and lo and behold, it worked.

我是如何根据Android开发者网站上的一步一步的说明知道这一点的,这对我来说是一个谜。我之所以决定更改targetSdkVersion,是因为问题中引用的页面的最后一部分。它是这样写的:“如果您正在增加现有应用程序与支持库之前版本的Android API的向后兼容性,请确保更新应用程序的清单。”现在我没有增加现有应用程序的向后兼容性。我正在修改一个以2.3.3为目标的现有应用程序,使其能够包含操作条(看起来更像是“向前兼容”)。我试着升级了targekSdkVersion,虽然我的想法已经过时了,但是,它成功了。

#2


3  

Don't know if it helps you at all, but I've been struggling with a similar issue for several hours and finally managed to resolve it. In my case, inside my own project's styles.xml file, I was referencing the AppCompat style in the wrong way in the parent attribute.

我不知道这对你是否有帮助,但我已经为一个类似的问题挣扎了几个小时,最后终于解决了它。就我而言,在我自己的项目风格中。xml文件,我在父属性中以错误的方式引用AppCompat样式。

I was using:

我用:

<style name="AppBaseTheme" parent="android:Theme.AppCompat">

where I should have used:

我应该使用的地方:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat">

My mistake was in the reference to the theme.

我的错误是涉及到主题。

#3


1  

I found that setting the Android target to 2.3.3 for the library project android-support-v7-appcompat solves this issue. You don't need to set your project's target higher, but the library's target lower.

我发现,将Android目标设置为2.3.3的库项目Android -support-v7-appcompat可以解决这个问题。您不需要将项目的目标设置得更高,但是库的目标要更低。

#4


0  

You have pinpointed the all important document and the relevant section "adding libraries with resources":

你已确定所有重要文件及有关“增加图书馆资源”一节:

http://developer.android.com/tools/support-library/setup.html

http://developer.android.com/tools/support-library/setup.html

The error suggests that your project is not able to find these resources. Please double check the following :

错误提示您的项目无法找到这些资源。请仔细检查以下内容:

  1. You have checked "is library" on the library project

    您已经在库项目中查询了“is library”

  2. You have exported the jars IN THE LIBRARY PROJECT as described in that section

    您已经在库项目中导出了jar,如该部分所述

  3. You have added the library project as a reference to your app project

    您已经添加了库项目作为您的应用程序项目的参考

  4. You have added the android-support-v4.jar to your libs folder in your app project

    您已经添加了android-support-v4。jar到你的libs文件夹在你的应用项目。

If this is all definitely correct, check eclipse for any error messages - is it the library project or the app project that lights up like the proverbial Christmas Tree ?

如果这一切都是绝对正确的,请检查eclipse是否有任何错误消息——是库项目还是像众所周知的圣诞树一样点亮的应用项目?

#5


0  

First of all check if you have done all the described steps as explained on the developers site. Then, for the errors :

首先检查您是否已经完成了开发人员站点上所描述的所有步骤。然后,对于错误:

Unable to resolve target 'android-16'

无法解析目标“android-16”

Make sure that you have installed API16, this errors occurs if you have mentioned your targetsdk to 16 in your Manifest while the sdk for API16 is not installed. You can either install the API or edit the targetApi of your project to the minimum required version, for the support library you are using I think API 11 is the minimum required android version.

确保您已经安装了API16,如果您在清单中提到了targetsdk到16,而API16的sdk没有安装,则会出现此错误。您可以安装API,也可以将项目的targetApi编辑到最小需要的版本,对于您正在使用的支持库,我认为API 11是最小需要的android版本。

android:Widget.Holo.ActionBar

android:Widget.Holo.ActionBar

This error should also be fixed once the targetsdk is fixed in your project. Since to use Holo.ActionBar you'l need to target your project to minimum supported api level ie. 11 or higher.

一旦targetsdk在您的项目中得到修复,这个错误也应该得到修复。因为使用完全。ActionBar你需要将你的项目定位到最小支持的api级ie。11或更高。

#6


0  

I met similar problems. Using API 18, ADT.

我遇到了类似的问题。ADT使用API 18日。

To solve the "Unable to resolve target 'android-16' problem", I remove the import lib project and repeat what the tutorial says.

为了解决“无法解决目标‘android-16’问题”,我删除了import lib项目,并重复本教程中的内容。

Then I found I couldn't really "add" the library to my project. (The instruction "In the Library pane, click Add." there is not that detail. 1. In your project -> properties -> Java build path -> Libraries -> Add Library... -> Android Classpath Container -> Select the lib project 2. In your project -> properties -> Android -> add... -> choose the lib -> apply

然后我发现我不能真正地“添加”这个库到我的项目中。(指令“在库窗格中,单击Add.”,没有那个细节。1。在您的项目中—>属性—> Java构建路径—>库—>添加库…-> Android类路径容器->选择lib项目2。在你的项目中->属性-> Android ->添加…->选择lib ->应用

cheers

干杯

#7


0  

Here is how I avoided the error, " Unable to resolve target 'android-16'", when adding v7.

下面是我如何避免这个错误,“无法在添加v7时解析目标‘android-16’”。

First, I followed the instructions for adding support libraries with resources in eclipse. Following step 4, I clicked Finish. That's when I saw the error message, android-support-v7-appcompat] Unable to resolve target 'android-16'.

首先,我遵循了在eclipse中添加资源支持库的说明。在步骤4之后,我单击Finish。这时我看到了错误信息,android-support-v7-appcompat无法解析目标“android-16”。

Second, I read this post. I also opened the download manager, but did not need anything updated. In order to make sure that I followed the steps exactly as correctly as I could, I deleted the android-support-v7-appcompat project and started over.

第二,我读了这篇文章。我也打开了下载管理器,但是不需要任何更新。为了确保尽可能正确地遵循这些步骤,我删除了android-support-v7-appcompat项目并重新开始。

This second time I didn't check any different boxes, after all. But, I also got no errors at step 4. Who can say why?

这第二次我没有检查任何不同的盒子,毕竟。但是,我在第4步也没有错误。谁能说为什么?

Thanks, Dewald, you expressed in this question the same frustration I have. Is experience the only way out?

谢谢,Dewald,你在这个问题上表达了我同样的沮丧。经验是唯一的出路吗?

#8


0  

If some of your resource xml files cannot find their respective AppCompat references, make sure you also add the support libraries to your project properties. You do this by:

如果您的一些资源xml文件无法找到它们各自的AppCompat引用,请确保您也将支持库添加到项目属性中。你这样做:

1.) right clicking on your project in the project

1.)右键单击项目中的项目

2.) Select properties. (Bottom of the menu)

2)。选择properties。(底部的菜单)

3.) click Android on the left hand side menu of the new pop up window

3)在新弹出窗口的左手边菜单上点击Android

4.) In the libraries section, click the "add button"

4)。在libraries部分,点击“添加按钮”

5.) Select the respective libraries. (If none show up, check to see if they are already displayed in the "libraries" window of the pop up menu. If there is a red x by the desired library, try and resolve that issue first.)

5)。选择相应的库。(如果没有显示,请检查它们是否已经显示在弹出菜单的“libraries”窗口中。如果想要的库中有一个红色的x,请先尝试解决这个问题。

6.) Once there is a green checkmark next to the desired libraries, make sure the "Is Library" option in the libraries section IS NOT selected.

6)。一旦在所需的库旁边有一个绿色的复选标记,请确保在libraries部分中的“is Library”选项没有被选中。

7.) Click apply.

7)。单击apply。

8.) Close the pop up menu.

8)。关闭弹出菜单。

If this doesn't work, let me know and I can help you out. I spent three hours on the problem so I know pretty much all the possible things that can go wrong with the set up.

如果不行,告诉我,我可以帮你。我在这个问题上花了三个小时,所以我几乎知道所有可能出现的问题。

#9


0  

Go to Project -> uncheck Build Automatically

去项目->取消自动构建。

Go to Project -> Clean... , clean both the library project and your app project

去项目->清洁…,清理库项目和应用程序项目

Export your app as a signed APK while Build Automatically is still disabled

将应用程序导出为已签名的APK,而Build仍然是禁用的