昨天發生了這種情況,當時我正在替換XML中的一些元素。當我參加我的主課時,我看到所有對R的引用都被標記爲紅色,並且我被告知R無法解析爲變量。起初,我試圖重建和清理我的項目,但那不起作用,所以我認爲問題在於我最近編輯的XML文件,但我現在知道問題出在哪裏。是的,我確信android.R沒有被導入。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文件。我正在評論,所以如果有人解決了這個問題,我可以稍後再看。 – Dyrborg 2014-10-27 21:04:27
[「R無法解析爲變量」?](http://stackoverflow.com/questions/7824730/r-cannot-be-resolved-to-a-variable) – 2014-10-27 21:09:24