1

While working in Android Studio, I accidentally clicked the button "Sync Project with Gradle Files" in the toolbar. Now all instances of R are highlighted in red as "Cannot resolve symbol R". Additionally, all folders but the app name in the directory structure toolbar are red-underlined, and I have a message saying that the IDs for three checked radio buttons in one of my XML layout files can't be found (even though they're a few lines below). There's not even an R.java anywhere in the project structure. Restarting Android Studio did not fix this; neither did pressing the same button again.

Edit: I'd hoped to be able to copy all my files to a new project, but the problem persists in new projects, too.

Here is my XML file; I have removed the checked-by-default buttons:

<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"
    android:screenOrientation="landscape"
    tools:context="com.cmpt276.darren.minions.OptionsMenu">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:id="@+id/linearLayout2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/num_rows"
            android:id="@+id/textView8"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true" />
        <RadioGroup android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <!--//android:checkedButton="@id/rows3"-->

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/rows3"
                android:id="@+id/rows3" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/rows4"
                android:id="@+id/rows4" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/rows8"
                android:id="@+id/rows8" />
        </RadioGroup>


    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/num_minions"
            android:id="@+id/textView9" />
        <RadioGroup android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <!--android:checkedButton="@id/numMinions6"-->

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minions_6"
            android:id="@+id/numMinions6" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minions_10"
            android:id="@+id/numMinions10" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minions_15"
            android:id="@+id/numMinions15" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minions_20"
            android:id="@+id/numMinions20" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/num_columns"
            android:id="@+id/textView10"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <RadioGroup android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="@string/num_columns">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/columns4"
                android:id="@+id/columns4" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/columns6"
                android:id="@+id/columns8" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/columns12"
                android:id="@+id/columns12" />
        </RadioGroup>
    </LinearLayout>

</RelativeLayout>
EMBLEM
  • 2,207
  • 4
  • 24
  • 32
  • how about clean and build? – Randyka Yudhistira Feb 18 '15 at 04:32
  • file -> invalidate caches and restart might solve the problem too – Illegal Argument Feb 18 '15 at 04:33
  • I often see funky things around R if one of my layout files can't be compiled. – Mike Cole Feb 18 '15 at 04:36
  • @IllegalArgument That just made it worse. Now the rendering of my layout is just tiny black text on a plain gray background. And there are apparently 57 missing styles errors. – EMBLEM Feb 18 '15 at 04:39
  • @EMBLEM I guess the nexus phone surrounding layout is gone. Try compiling and running the code if there is no errors I see this behavior sometimes but it didnot stop me from compiling are there any more errors? – Illegal Argument Feb 18 '15 at 04:42
  • @IllegalArgument Like I said, I have a message saying that the IDs for three checked [-by-default] radio buttons in one of my XML layout files can't be found (even though they're a few lines below). The project does not compile. – EMBLEM Feb 18 '15 at 04:44
  • did you check stuff like cyclic dependencies and doing a `android:layout_below="@id/id_below_object"` and then the object below it is `android:id="@+id\id_below_object"` – Droidekas Feb 18 '15 at 04:55
  • and can you please add your xml code – Droidekas Feb 18 '15 at 04:55
  • @Droidekas XML added; I commented or removed what was causing the errors, but you can probably get the gist of it. – EMBLEM Feb 18 '15 at 05:02
  • @EMBLEM did removing the erroneous code help when you rebuild it? – Droidekas Feb 18 '15 at 05:03
  • @Droidekas As it turns out, yes. However, there is no indication that Android Studio is either aware or unaware of R.java. It is neither syntax highlighted nor marked as an error. – EMBLEM Feb 18 '15 at 05:07
  • @EMBLEM XML issues are not well understood by Android Studio or eclipse.Your best bet would deleting things individually and figuring out the erroneous code.these errors (in eclipse would normall be mentioned below the WYSIWYG editor)I am not sure of the interface in Android Studio – Droidekas Feb 18 '15 at 05:13

2 Answers2

1

refer here

XML issues will prevent your R file from building.So if would be ideal to go through it with a comb.In your code

<RadioGroup android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkedButton="@id/rows3"// error line
        >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rows3"
            android:id="@+id/rows3" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rows4"
            android:id="@+id/rows4" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rows8"
            android:id="@+id/rows8" />
    </RadioGroup>

The error line is currentlyreferring to an id which has not been currently assigned.This creates and error in your xml and hence the R does not the buil.

This solution indicates that you need to mention the property as

  android:checkedButton="@+id/rows3"//

This should work.Or else please define it programmatically.That would just avoid any such issues.

Community
  • 1
  • 1
Droidekas
  • 3,464
  • 2
  • 26
  • 40
0

Already answerd this in here:
Android Studio: Gradle Build Problems/"R can't be resolved"

Part of that answer:

  1. Look, if you don't write any code in xml file then just click rebuild or make project button or clean button. Your problem may be solved.
  2. If you modify your xxxxx.xml layout file then look these files and I'm sure you've written some wrong code. Like theme name problem, any referencing problem of strings, dimens, colors and so on. Correct them and may be your R can't be resolved text will be solved. Obviously, after doing this click make project button and provably get no error..
Community
  • 1
  • 1
Maniruzzaman Akash
  • 4,610
  • 1
  • 37
  • 34