R cannot be resolved to a variable

后端 未结 29 1064
不知归路
不知归路 2020-11-28 14:14

I would like to fix this error

R cannot be resolved to a variable

I looked up many answers, but I could not get the right one;

相关标签:
29条回答
  • 2020-11-28 15:05

    Tip: update SDK and all utilities else one time. It helped to me.

    0 讨论(0)
  • 2020-11-28 15:06

    I had the same issue and main reason why R.java was not re-generated is errors in any of the files in the project. Once we fix those the R.java should get generated.

    0 讨论(0)
  • 2020-11-28 15:08

    Just look in to your header files there will be:

    import somefile.R;
    

    Just remove that line and that's it.

    0 讨论(0)
  • 2020-11-28 15:10

    I had the same problem. I didn't have any lines stating "import something.R". Cleaning and rebuilding also didn't solve the issue.

    It turned out to be because of the name of a layout xml file. Those files can only contain lowercase letters, or numbers, or underscores. When I changed the filename (and everything that refers to it) from myLayout.xml to my_layout.xml, everything worked.

    0 讨论(0)
  • 2020-11-28 15:10

    Nothing mentioned above worked for me. However, I fixed it myself.

    Solution:

    If you've any other files except activity_main.xml / main.xml inside app_name/res/layout remove it and try clean and build.

    Note:

    The unwanted files or (rather used later) files inside app_name/res/layout would be *.out.xml. Remove the same.

    0 讨论(0)
  • 2020-11-28 15:11

    what did it for me...

    when i checked my project properties file (/project_root/project.properties), it had a reference to an old android version target which i no longer had installed

    # Project target.
    target=android-15
    

    as soon as I changed this to say 17, it worked perfectly, soon as the file was saved my gen folders were built and inflated.

    # Project target.
    target=android-17
    
    0 讨论(0)
提交回复
热议问题