我打开了我的旧项目,它在几个月之前正常工作。
现在,我几乎在每个活动中都会发现错误或与错误相关的内容 - R cannot be resolved to a variable
。
我在stackoverflow上做了所有操作,但还没有任何工作:
我认为问题可能出在其中一个XML文件中,其中还包含3个错误,例如No resource identifier found for attribute "atribute" in package my.package
在本次活动中,我使用的是facebook样本来分享内容。 代码:
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2010-present Facebook.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:id="@+id/activity_share_ui_container">
<com.facebook.widget.LoginButton <!-- 2 errors here --->
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
facebook:confirm_logout="false"
facebook:fetch_user_info="true"
/>
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/greeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:textColor="#333"
android:textSize="18sp"/>
<com.facebook.widget.ProfilePictureView <!-- 1 error here --->
android:id="@+id/profilePicture"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="10dp"
facebook:preset_size="normal"/>
<Button
android:id="@+id/postStatusUpdateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Post Status Update"
/>
<Button
android:id="@+id/postPhotoButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Post Photo"
/>
<Button
android:id="@+id/pickFriendsButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pick Some Friends"
/>
<Button
android:id="@+id/pickPlaceButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pick Place"
/>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
属性有问题:
我错过了什么?
答案 0 :(得分:4)
您在xml文件中使用了facebook UI元素。看来您的android项目无法获取FaceBook SDK的引用,因为您可能已将其删除或移动到某处。您可以通过以下方式验证它:
项目探索项目的权利 - &gt;选择属性 - &gt;选择Android - &gt;检查是否有正确的FaceBook SDK参考。
一旦你提供了正确的引用......构建错误将在清理和构建后消失。
答案 1 :(得分:2)
已经给出了答案,所以这只是为了澄清错误发生的原因:
只有在编译完所有资源后才能生成R文件。正如您所注意到的,您的资源中存在错误(可能与正确包含FB SDK有关),这会阻止生成R.java文件。由于该文件不存在,因此无法解析R cannot be resolved to a variable
。
Eclipse提供导入类似命名的类(android.R)或创建名为R的东西(无论是类,枚举,变量......),这些解决方案都不是你需要的情况下。
从您提供的屏幕截图中,导入FB SDK有问题。选择“删除”,然后重新添加。
答案 2 :(得分:1)
试试这个:
答案 3 :(得分:0)
首先检查您的xml文件,确保它们有效并且不会产生任何错误
然后,尝试使用完全限定的包名称导入R,如下所示:
import.com.xyz.R;
然后保存文件。