Android无法解析R
昨天发生了这种情况,当时我正在替换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>
我无法看到此xml文件中的任何问题。你也可以发布你使用它的类文件的代码,并检查你是否使用android.r
并使用com.yourapp.r
。这可能是这种情况。你是否也可以发布该课程
当我去重新打开项目和类文件向你展示,我试着再次导入类R(它甚至不会显示),它显示出来,一切似乎都已修复! – 2014-10-27 21:10:14
是的,它可能工作的进口快乐帮助;)马克&upvote如果这是有用的 – BackStabber 2014-10-27 21:11:46
随时与我发生我们所有的时间我们复制批次:D – BackStabber 2014-10-27 21:12:17
在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