59

I was working on my app and everything was normal until I tried to display image in java.

I ran the app once and it ran normally, the picture was displayed. After that it asked me to import some libraries and I imported them. After that I got errors for my activities.

Errors like:

Gradle: error: cannot find symbol variable activity_main
Gradle: error: cannot find symbol variable button1
Gradle: error: cannot find symbol variable button2
Gradle: error: cannot find symbol variable textView
Gradle: error: cannot find symbol variable secondActivity

In MainActivity I have imported these libraries:

import android.R;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;

and in secondActivity these:

import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

Does anyone know how to fix this?

EDIT: I deleted import android.R; and now it works normally.

Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
user2668638
  • 653
  • 1
  • 7
  • 9

12 Answers12

75

You shouldn't be importing android.R. That should be automatically generated and recognized. This question contains a lot of helpful tips if you get some error referring to R after removing the import.

Some basic steps after removing the import, if those errors appear:

  • Clean your build, then rebuild
  • Make sure there are no errors or typos in your XML files
  • Make sure your resource names consist of [a-z0-9.]. Capitals or symbols are not allowed for some reason.
  • Perform a Gradle sync (via Tools > Android > Sync Project with Gradle Files)
Community
  • 1
  • 1
BLaZuRE
  • 2,356
  • 2
  • 26
  • 43
  • 1
    Manually declaring the "import com.your.app.package.name.R" fixed the "cannot find symbol variable" for me. But this was after i had copied a class from a module into the main app project in Android Studio and Gradle failed to build. But I would suggest trying the above options first, as that is the likely case for this error. – TouchBoarder Oct 31 '15 at 15:32
  • 2
    I had similar problem currently. The problem was that in my project I had more modules. E.g. `com.packagename.app` module and `com.packagename.library` module. Although both, the class and the `R.string` resource were from the `com.packagename.app` module I somehow had `import com.package.library.R` in the class. Somehow Android Studio did not recognize this as an error and Gradle/Sync build my project without problems. But when I tried to run Unit Tests Gradle build failed with the described error. Removing the `import com.packagename.library.R` solved my problem. – Jan Kubovy Jan 17 '16 at 09:06
  • Note: underscore `_` can also be used in resource id's – Nino van Hooff Aug 04 '17 at 08:49
  • Actually, you do need to import R if the class you are working on is not in the application package. – Brill Pappin Nov 29 '17 at 14:45
  • You just saved me. I moved a class from a module to main app folder and also the strings from the module and I couldn't understand why the strings are not found in the class I moved. Issue: the class imported R from the module, not from the app. Thanks a lot! – sunlover3 Jan 22 '18 at 11:12
25

If you are using multiple flavors?

-make sure the resource file is not declared/added both in only one of the flavors and in main.

Example: a_layout_file.xml file containing the symbol variable(s)

src:

flavor1/res/layout/(no file)

flavor2/res/layout/a_layout_file.xml

main/res/layout/a_layout_file.xml

This setup will give the error: cannot find symbol variable, this is because the resource file can only be in both flavors or only in the main.

TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
23

If you are using a String build config field in your project, this might be the case:

buildConfigField "String", "source", "play"

If you declare your String like above it will cause the error to happen. The fix is to change it to:

buildConfigField "String", "source", "\"play\""
Sean Goudarzi
  • 1,244
  • 1
  • 10
  • 23
2

Open project in android studio click file and click Invalidate Caches/Restart

RanaUmer
  • 561
  • 5
  • 9
1

make sure that the imported R is not from another module. I had moved a class from a module to the main project, and the R was the one from the module.

Alberto M
  • 1,608
  • 1
  • 18
  • 42
0

Make sure you have MainActivity and .ScanActivity into your AndroidManifest.xml file:

<activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ScanActivity">

    </activity>
julien bouteloup
  • 3,022
  • 22
  • 16
0

Make sure your variables are in scope for the method that is referencing it. For example I had defined a textview locally in a method in the class and was referencing it in another method.

I moved the textview definition outside the method right below the class definition so the other method could access the definition, which resolved the problem.

Jazzmine
  • 1,837
  • 8
  • 36
  • 54
0

Another alternative to @TouchBoarder's answer above is that you may also have two layout files with the same name but for different api versions. You should delete the older my_file.xml file

my_file.xml
my_file.xml(v21)
Bond James
  • 33
  • 2
0

Make sure that the control you are referring to in the XML layout file really exists, even if it's not being used.

I had this after when I did "removed unused resources" refactory.

For example, you will get this error if there is no progressBar in the layout.

rootView = inflater.inflate(R.layout.fragment_stats, container, false);

mProgressBar = rootView.findViewById(R.id.progressBar);
live-love
  • 48,840
  • 22
  • 240
  • 204
0

Please check you accidentally added the XML file into the layout folder.

jerald jacob
  • 613
  • 1
  • 4
  • 18
0

Make Sure your package name correct at top, wrong package name may cause this issue.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
fazal ur Rehman
  • 330
  • 2
  • 5
0

Check if it's an interoperability issue, for example, the class not found may be a .java file not .kt

Lucas B.
  • 489
  • 5
  • 15