Cannot Resolve symbol R

后端 未结 8 1816
礼貌的吻别
礼貌的吻别 2021-01-18 07:02

I was working on Android Studio for adding a short toast message.(I was making an Android Wear Application)

I couldn\'t know why this code has error on \'symbol R\'

相关标签:
8条回答
  • 2021-01-18 07:16

    I resolved it by editing my build.gradle file.

    The compiledSdkVersion must align with your appcompat dependency.

    My compileSdkVersion was 22 so I edited my appcompat dependency to

    compile 'com.android.support:appcompat-v7:22.2.1'
    

    So, I hope this can be a solution.

    0 讨论(0)
  • 2021-01-18 07:17

    Just in case any of the above answers don't work, the following solved the issue in my case...

    Here's what I tried:

    • Multiple "cleans", "rebuilds" and "invalidate caches restart"

    • Attempts to manually add [my package].R,

    This didn't work, so I uninstalled Android Studio, and reinstalled the latest version.

    The accepted answer to the above link has the following steps (which I reproduce here):

    1. Uninstall from installer. (via control panel for windows)
    2. Remove Android Studio files

      Go to your user folder (%USERPROFILE%), and delete .android, .AndroidStudio and any analogous directories with versions on the end, i.e. .AndroidStudio1.2, as well as .gradle and .m2 if they exist. Then go to %APPDATA% and delete the JetBrains directory. Finally, go to C:\Program Files and delete the Android directory.

    3. Remove the SDK

      To delete any remains of the SDK, go to %LOCALAPPDATA% and delete the Android directory.

    4. Delete Android Studio Projects

      Android Studio creates projects in a folder %USERPROFILE%\AndroidStudioProjects, which you may want to delete.

    I skipped 4), then uninstalled a separate Java 7 installation I had, for good measure.

    Next I downloaded the latest Android Studio (3.3.2) and installed it.

    A bit drastic perhaps, but now everything is working and the "Cannot resolve symbol R" error is gone.

    0 讨论(0)
  • 2021-01-18 07:23

    If in your case in imports R is missing you Have to import R manually.

    import <packagename>.R;
    

    Replace <packagename> with your own package name.

    In your code, R was not imported, therefore your Android Studio might got confused to find resources in your package.

    0 讨论(0)
  • 2021-01-18 07:27

    I know others have said most of these points, but this should serve as a sum-up and hopefully bring up some new solutions!

    If only the letter 'R' is red, it means there is something wrong with your R file (found at build\generated\source\r\debug\com\domain.projectname\R). Don't go around editing your R file- because each time the project is cleaned and built/rebuilt the R file regenerates (it completely resets without fail). There are a few possible solutions:

    1) You might have written import android.R; at the top of your Activity. You need to have a different R file imported. This means if your domain is "example" and your project name is "project" then your import should be: import com.example.project.R

    2) R is a build file. That means it disappears when you clean and generates anew when you build, as previously said above. Make sure you build or rebuild your project after cleaning it.

    3) Maybe your gradle project sync has not completed. Wait until all processes have finished running!

    4) It is also possible your R file is corrupt or missing. If the above don't work, you can come to this conclusion. It has a very easy fix, because as I explained earlier when you rebuild a new R file is generated. So just navigate to Build and click Clean project, then when that's finished, click Rebuild project. With the new R file your problems should be gone.

    P.S: It's nearly impossible to give an accurate answer without all relevant code provided

    0 讨论(0)
  • 2021-01-18 07:29

    It seems like Gradle is taking too long time to execute for your computer. Wait for the Gradle to finish building the project.

    0 讨论(0)
  • 2021-01-18 07:30

    Below some procedures are mentioned :

    1) Try Build -> Clean/Rebuild project .

    2) Manually typing import <package name>.R, If in import R is missing.

    3) complete all resources of its corresponding XML file .

    I hope any one of them would be helpful for you as there is no Particular way to resolve this issue.

    0 讨论(0)
提交回复
热议问题