R.java file not updating with UI element's id

2020-03-01 05:54发布

I added some UI elements to the main.xml file in the res\layout folder and when I try to access them through the call, R.layout.my_uielement, the UI element that I created is not there. Even when I add a new xml file with UI elements, it still doesn't show up in the R.layout class. I have made checked the ids on them and they have the correct format (I think): android:id="@+id/my_button". What could be the problem? Do I need to compile the code first?
UPDATE:
I have already tried adb kill-server then adb start-server. It doesn't seem to help. I have also tried R.id.my_uielement, it doesn't register either.

标签: android adb
8条回答
贼婆χ
2楼-- · 2020-03-01 06:02

Clean project works fine. In Eclipse if you just change some xml components, it doesn't always generate a new R file. Cleaning does it.

查看更多
戒情不戒烟
3楼-- · 2020-03-01 06:05

I've noticed that my R.java file is not always being properly updated when I add items to my strings.xml file or elsewhere. If I use a new value I added like R.string.newString the actual integer value might still be pointing to R.string.oldString or whatever (it seems to usually be the element directly above or below the new element in strings.xml but I haven't nailed down the pattern for sure yet).

You're not supposed to modify the R.java file, but it doesn't say anything about deleting it (not that I've seen, anyway). I've found that deleting R.java usually fixes my resource-related problems because Eclipse immediately regenerates the file with the integer values properly assigned.

Please note, I'm very much a beginner at Eclipse so I may not understand the consequences of deleting R.java but it seems to be working well for me so far. If what I'm doing is very bad, please let me know.

查看更多
三岁会撩人
4楼-- · 2020-03-01 06:08

Use R.id.your_ui_element, not R.layout.your_element

查看更多
倾城 Initia
5楼-- · 2020-03-01 06:17

After 2 days I found a way to fix it...

I had saved half done project in my mail, so when R.java vanished i just replaced it with the old R.java file, this fixed a lot of errors, then made necessary changes and built the project(build it not clean)...I do not know whether clean and build performs the same job or not..but everytime i cleaned it , R.java used to go invisible...

查看更多
对你真心纯属浪费
6楼-- · 2020-03-01 06:22

I faced this problem too. The R.java was neither generated nor updated.

If R.java is not generated when you create the project, run your 'empty` project. An R.java with references to resources will be automatically generated.

If R.java is not getting updated, Check for updates in Help -> Check for Updates in Eclipse and update your SDK and ADT if required.

查看更多
Rolldiameter
7楼-- · 2020-03-01 06:22

Clean is working because it BUILD the project after cleaning. So the problem is ( at least it was in my case, because I switched off the Automatic Build a few days earlier ) that after you add an item or anything in a .xml file, the eclipse doesn't build it and can't refresh the R.java .

查看更多
登录 后发表回答