'R cannot be resolved to a variable' - gen folder is empty

前端 未结 14 2425
故里飘歌
故里飘歌 2021-02-13 22:34

I\'ve got an error:

R cannot be resolved to a variable

in line:

setContentView(R.layout.main);

but I can\'t r

相关标签:
14条回答
  • 2021-02-13 22:55

    Don't import Android.R;

    instead,

    import package.name.R; where package.name is your package name declared in the manifest.

    Android.R contains all the default layouts packaged with Android. Your custom layout will not be Android.R, it will be in package.name.R;

    0 讨论(0)
  • 2021-02-13 22:56

    On eclipse you have the "Problems" window. If you fix all the other problems in there other than R cannot be resolved, the "R" problem will be auto-fixed as R is not generated because most likely you have project related problems.

    Hope this helps. If you don't manage to do it, put a print of what your Problems window is showing.

    0 讨论(0)
  • 2021-02-13 22:56

    Problem is solved, but I don't know what has changed... I reinstalled Eclipse, SDK etc. and gen folder was empty as before, the same problem.

    My supervisor told me to do it all again, and it worked (for now). I don't know what was the cause. I though I'd had tried everything, but it's working now so I'll never know. Thanks for your help guys!

    0 讨论(0)
  • 2021-02-13 23:00

    Yeah, i got stuck on this for hours. My solution was to: delete all projects from workspace->re-import the desired project by going to file->import->Browse->selecting the project->and finally check the "add project to working sets box" then clicking finish. The .R errors i once had then disappeared. I hope this helps someone!

    0 讨论(0)
  • 2021-02-13 23:00

    Try this:

    1. First disable automatic build option from Project>Build Automatically
    2. Change eclipse work space
    3. Create a new Hello World test project
    4. Comment setContentView(R.layout.main);
    5. Build project manually by right click on your project in eclipse>Build project
    6. Check whether R.java in gen folder has been generated
    7. If yes, then uncomment setContentView(R.layout.main);
    8. Now build project again manually and run

    This should resolve the problem hopefully.

    0 讨论(0)
  • 2021-02-13 23:00

    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)
提交回复
热议问题