【发布时间】:2014-10-27 20:56:23
【问题描述】:
这发生在昨天我在我的 XML 中替换一些元素时。当我去我的主课时,我看到所有对 R 的引用都被标记为红色,并且我被告知 R 无法解析为变量。起初,我尝试重建和清理我的项目,但没有奏效,所以我假设问题出在我最近编辑的 XML 文件中,但我现在知道问题出在哪里。是的,我确实确保没有导入 android.R。
一些额外的细节:
- 我使用的是 Android 5.0 (API 21)
- R 问题未出现在我的其他类文件中,这些文件引用了 R。
这里是有问题的 XML 文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_marginTop="20dp"
android:text="@string/rye"
android:textColor="#888888"
android:textAppearance="?android:textAppearanceLarge" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:isIndicator="false"
android:numStars="5"
android:stepSize="1" />
<Button
android:id="@+id/dropDownButton"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignBottom="@+id/ratingBar1"
android:layout_toRightOf="@+id/ratingBar1"
android:onClick="dropDown"
android:text=">"/>
<RelativeLayout
android:id="@+id/dropDownLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/textView2"
android:visibility="gone" >
<TextView
android:id="@+id/testTV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Testing dropdown" />
</RelativeLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tipTitle"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#888888"
android:layout_below="@+id/quizFragment"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<FrameLayout
android:id="@+id/quizFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/dropDownButton" />
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/people_dining_title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#888888"
android:layout_below="@+id/seekBar3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/subtotalTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/subtotal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#888888"
android:layout_below="@+id/seekBar2"
android:layout_alignLeft="@+id/seekBar2"
android:layout_alignStart="@+id/seekBar2" />
<TextView
android:id="@+id/totalTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/subtotalText"
android:layout_below="@+id/subtotalText"
android:layout_marginTop="10dp"
android:text="@string/total"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#888888" />
<TextView
android:id="@+id/totalText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/subtotalText"
android:layout_alignRight="@+id/subtotalText"
android:layout_below="@+id/totalTitle"
android:focusable="false"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffff000c" />
<EditText
android:id="@+id/subtotalText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/subtotalTitle"
android:ems="10"
android:hint="SUBTOTAL"
android:inputType="numberDecimal"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ff0000"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/seekBar2"
android:layout_alignEnd="@+id/seekBar2">
</EditText>
<TextView
android:id="@+id/eppTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/totalText"
android:layout_below="@+id/totalText"
android:layout_marginTop="10dp"
android:text="@string/epp"
android:textColor="#888888"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="@+id/eppText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/eppTitle"
android:layout_alignRight="@+id/totalText"
android:layout_below="@+id/eppTitle"
android:focusable="false"
android:padding="2dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/buttonDone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/eppText"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:onClick="done"
android:text="@string/done"
android:textStyle="bold" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekBar2"
android:layout_below="@+id/TextView02"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekBar3"
android:layout_below="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
【问题讨论】:
-
这在 Eclipse 中有时也会发生在我身上(你在使用它吗?),我通常最终会大发雷霆并从头开始创建一个新项目,然后只导入所有 .java 和 .xml 文件。我正在评论,所以如果有人有解决问题的方法,我可以稍后再看。
标签: android xml r.java-file