代码之家  ›  专栏  ›  技术社区  ›  krzakov

R无法解析为变量,XML问题?

  •  2
  • krzakov  · 技术社区  · 12 年前

    我打开了我的旧项目,几个月前它运行正常。 现在,我几乎在每个活动中都有eclipse错误或与错误有关的东西- R cannot be resolved to a variable .

    enter image description here

    我在stackoverflow上做了所有准备好的动作,但还没有奏效:

    • 已检查导入
    • 项目已清理

    enter image description here

    我认为问题可能出现在其中一个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>
    

    财产有问题: enter image description here

    我错过了什么?

    4 回复  |  直到 12 年前
        1
  •  4
  •   user1288005    12 年前

    您已经在xml文件中使用了facebookUI元素。你的android项目似乎无法获得FaceBook SDK的引用,因为你可能已经将其删除或移动到了某个地方。您可以通过以下方式进行验证:

    右键单击“项目探索”中的项目-->选择“财产”-->选择安卓系统-->检查是否有正确的FaceBook SDK参考。

    一旦你提供了正确的参考。。。清理和构建后,构建错误将消失。

        2
  •  2
  •   njzk2    12 年前

    已经给出了答案,所以这只是为了澄清错误发生的原因:

    只有在编译了所有资源的情况下,才能生成R文件。正如您所注意到的,您的资源中存在错误(可能与FB SDK的正确包含有关),这些错误阻止了R.java文件的生成。由于该文件不存在,因此无法解析 R cannot be resolved to a variable .

    Eclipse提供导入一个类似名称的类(android.R)或创建一个名为R的东西(无论是类、枚举、变量…),在这种情况下,这些解决方案都不是您所需要的。

    从您提供的屏幕截图来看,FB SDK的导入有问题。选择“删除”,然后再次添加。

        3
  •  1
  •   Philipp Jahoda    12 年前

    试试这个:

    • 导入Facebook 图书馆
    • 清洁的 你的项目
    • 进口 com.your.backage.R(您需要使用资源的位置)
        4
  •  0
  •   Bob Gilmore Brad    12 年前

    首先检查xml文件以确保它们是有效的,并且不会产生任何错误

    然后,尝试导入具有完全限定的程序包名称的R,如下所示:

    import.com.xyz.R;
    

    然后保存文件。