“R cannot be resolved to a variable”? [duplicate]

2018-12-31 05:48发布

This question already has an answer here:

In Eclipse, I've created a project from a source and now it shows errors - "R cannot be resolved to a variable". From what I found here, I had cleared and rebuilt the project, but still the R file doesn't appear in the /gen folder.

Any ideas?

30条回答
旧时光的记忆
2楼-- · 2018-12-31 06:37

My setup was broken after a recent update where the SDK build tools need to be installed separately after the SDK installation. So build tools update + Eclipse restart fixed this for me.

Credit goes to the Android reply here

查看更多
不再属于我。
3楼-- · 2018-12-31 06:38

When other solutions fail, select your project and delete it. MAKE SURE YOU DO NOT REMOVE FILES FROM DISK.

Then use file>import>existing android code into workspace, and select the location of your project.

查看更多
步步皆殇っ
4楼-- · 2018-12-31 06:39

I know this is an old question but I just solved my own version of it and perhaps this might help someone.

After two days of tearing my hair out with this, I finally got around it by deleting the raw folder, then recreating it and dropping the file(s) back in.

After that, another Project > Clean and it at last compiled.

查看更多
高级女魔头
5楼-- · 2018-12-31 06:40

My problem was strange and took some time to find. Somehow the package of the src file changed so that the last entry in the package was deleted. So for example initially my class MyActivity.java was in package com.abc.client.test.app but after I added a user permission, the app got removed and the package was renamed to com.abc.client.test. I don't know how it happened. Renaming the package and putting the java file in the correct place fixed the problem.

查看更多
冷夜・残月
6楼-- · 2018-12-31 06:41

Project > Clean...

usually regenerates R.java file located in gen/com.mypackage

查看更多
听够珍惜
7楼-- · 2018-12-31 06:41

I got this error when I added a js file to the res folder.

An error indicator was set on the res folder icon. In all the classes where I used R I got this error.

I moved the js file to the assets folder and everything worked.

查看更多
登录 后发表回答