Android开发中 No resource found that matches the given name 'Theme.AppCom报错以及R.java无法生成


本文主要以OSChina【开源中国】的代码为例说明一下两个问题的解决方法:

     <1>error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCom

     <2>R.java无法生成

文章中用的代码是OSChina【开源中国】的源码: 
     下载地址:http://download.csdn.net/detail/guanxin31415/8650007 
文中加底色的段落代表是引用自其它地方的内容

1. 错误说明

          今天在研究OSChina【开源中国】APP源码的时候,在导入源码之后,发现项目报错 ,错误截图如下:

                                  
然后打开了几个报错的文件,发现大部分的错误都是R.java文件缺失引起的:如下图
                                        





于是在网上寻找解决R。java无法自动生成的办法,比较多的答案是:

方法:右击你的工程(项目)——>Android Tools——>Fix Project Properties 即可。

但是自己这么做也解决不了问题,后来想想,假如把R.java无法生成的原因搞清楚了,那也就找到解决办法了嘛,于是在网上寻找无法生成R.java的原因,大致总结了一下网上的答案:R.java无法生成的原因如下:

R.java这个文件是会自动生成的,但是如果你不小心xml文件写错了,或者不小心在编辑xml的时候点击了run(其实这个时候会运行这个xml文件,会生成一个同名的xml文件,后缀为xx.out.xml),或者含有除xml文件和图片文件以外的其他文件,或者有不能识别的图片。然后正巧你project——》clear了一下项目,你可能会发现gen下面的R.java的文件没有了。因为某个文件有错,导致R.java不能自动创建。那么是什么错呢:只要xml文件有问题,系统是绝对不会给你自动生成这个R.java文件,因为他要参照你的每张xml里的数据来生成R.java;drawable文件夹下有不能识别的文件或图片。

好吧,说的很有道理哈。。。也就是xml文件出问题了,恩恩,展开res文件夹,发现果然xml文件有不少报错,如下图:

                                 

打开之后发现错误在于: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.



          
在网上找到的解释是:

AppCompat is a library project. You need to reference the library project in your android project.

哦~也就是要把android-support-v7-appcompat.jar引用进来,很好,继续纠错。。。

以下是官方给出的  Adding libraries with resources的方法:

To add a Support Library with resources (such as v7 appcompat for action bar) to your application project:

Using 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>/ /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 library project folder 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 category panel on the left side of the dialog, select Android.
  3. In the Library pane, click the Add button.
  4. Select the library project and click OK. For example, the appcompat project should be listed as android-support-v7-appcompat.
  5. In the properties window, click OK.

Note: If you are using the android-support-v7-mediarouter support library, you should note that it depends on theandroid-support-v7-appcompat library. In order for the v7 mediarouter library to compile, you must import both library projects into your development workspace. Then follow the procedure above to add the v7 appcompat project as a library to the v7 mediarouter library project.


下面我用更直观一点的图文的形式说明以下我的做法:

1. 打开SDk maneger 更新sdk中的 support文件:

                    

1. File-》import-》Exisiting Android Code Into Workspace 

                      

点击Browse ,然后按照【 <sdk>/extras/android/support/v7/appcompat/】(<sdk>表示自己放sdk的路径),选中appcompat,然后确定,点击finish完成导入。导入之后发现android-support-v7-appcompat这个project也会报错:


我找的解决方法如下:

When using the v7-appcompat in Eclipse you have to use it as a library project. It isn't enough to just copy the *.jar to your /libs folder. Please read this (click) step-by-step tutorial ondeveloper.android.com in order to know how to import the project properly.

As soon as the project is imported, you'll realize that some folders in the /resfolder are red-underlined because of errors such as the following:

Errors in Eclipse

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

Solution

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.

按照上边的说法处理之后,android-support-v7-appcompat的错误解决了:

         


android-support-v7-appcompat的错误搞定之后,再回到OSChina的项目,项目右击选择properties,如下:

       

点击add,选中Android-support-v7-appcompat然后OK

 通过点击UP把Android-support-v7-appcompat移到第一位:

                 

点击OK;

至此error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.问题解决。


同时R.java也能自动完成了:

                 

/************************************************************************************************************************************

 *********************************  本人也还只是菜鸟一个,抱着互相学习的态度分享自己的代码学习之路*********************

 ********************************          文章中若有什么错误之处,还望各路大神吐槽指出            * *****************************

  ****************************************************************************************************************************************************************
*/ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值