IntelliJ Idea 不在 R.java 中生成 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13661169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ Idea not generate id in R.java
提问by Val
I recently switched to IntelliJ Idea 12.
我最近切换到 IntelliJ Idea 12。
Now created a project on android and immediately ran into a problem: not generated id in R.java
现在在android上创建了一个项目,立即遇到了一个问题:在R.java中没有生成id
Rebuild project does not help.
重建项目没有帮助。
Maybe in IntelliJ Idea have any subtlety is not known to me?
也许在 IntelliJ Idea 中有什么我不知道的微妙之处?
error:
错误:
12-01 16:08:15.067: ERROR/AndroidRuntime(7108): FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:266)
at android.widget.TextView.setText(TextView.java:3624)
at com.example.poem_new.MyActivity.invalidateCounters(MyActivity.java:41)
at com.example.poem_new.MyActivity.access /* This stub is for using by IDE only. It is NOT the R class actually packed into APK */
public final class R {
}
0(MyActivity.java:11)
at com.example.poem_new.MyActivity.onTextChanged(MyActivity.java:29)
at android.widget.TextView.sendOnTextChanged(TextView.java:8027)
at android.widget.TextView.handleTextChanged(TextView.java:8074)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8454)
at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:669)
at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:188)
at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:149)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:315)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
R.java file is empty:
R.java 文件为空:
##代码##采纳答案by Aleksander Adamowski
What you're observing is probably the by-design behaviour in Idea 12:
您所观察到的可能是 Idea 12 中的设计行为:
http://youtrack.jetbrains.com/issue/IDEA-99045
http://youtrack.jetbrains.com/issue/IDEA-99045
"It is behaviour of IDEA 12, it doesn't generate full R.java in gen folder, it generates it in the IDEA's system dir during compilation."
“这是 IDEA 12 的行为,它不会在 gen 文件夹中生成完整的 R.java,而是在编译期间在 IDEA 的系统目录中生成它。”
In your case, look into ~/.IntelliJIdea12\system\compiler\YOUR_PROJECT_NAME.RANDOM_STRING\.generated\aapt\YOUR_PROJECT_NAME.RANDOM_STRING\production\package\R.java
在你的情况下,看看 ~/.IntelliJIdea12\system\compiler\YOUR_PROJECT_NAME.RANDOM_STRING\.generated\aapt\YOUR_PROJECT_NAME.RANDOM_STRING\production\package\R.java
This file should be up to date and it should contain all the IDs you expect.
此文件应该是最新的,并且应该包含您期望的所有 ID。
It should also be hooked up to Idea's functionalities of autocompletion, usage search etc by some sort of internal Idea magic.
它还应该通过某种内部 Idea 魔法连接到 Idea 的自动完成、使用搜索等功能。
The empty R.java is normal (although misleading, and Jetbrains intends to change this behaviour).
空的 R.java 是正常的(虽然误导,Jetbrains 打算改变这种行为)。
回答by PNeves
I've run into the same problem. Being tired of Eclipse, I decided to try a new IDE. I created a new project for Android and inside a new module (trying to make Beginning Android examples to compile and run). When I created a new module it created the usual directories that you expect, including gen. I copied the source files, the manifest and the res directory. Run into this issue, were IDEA could not figure out what R. was. I did the following:
我遇到了同样的问题。厌倦了 Eclipse,我决定尝试一个新的 IDE。我为 Android 创建了一个新项目,并在一个新模块中创建了一个新项目(尝试编译和运行 Beginning Android 示例)。当我创建一个新模块时,它创建了您期望的常用目录,包括 gen。我复制了源文件、清单和 res 目录。遇到这个问题,IDEA 无法弄清楚 R. 是什么。我做了以下事情:
- remove import of android.R
- delete gen directory
- 删除 android.R 的导入
- 删除gen目录
After a clear build gen reappeared and the project was ok.
在一个清晰的构建 gen 重新出现并且项目正常之后。
Hoje it helps somebody.
Hoje 它可以帮助某人。
回答by Ali
Closing all intellij windows, renaming the project folder and opening it again solved my problem.
关闭所有 intellij 窗口,重命名项目文件夹并再次打开它解决了我的问题。
UPDATE:
更新:
Intellij tends to save generated files in it's own folder in users documents. So another way for this situation is to invalidate cache files. Go to file menu and select invalidate caches, then intellij will restart and it regenerate caches and other required files and everything will be ok!
Intellij 倾向于将生成的文件保存在用户文档中它自己的文件夹中。因此,这种情况的另一种方法是使缓存文件无效。转到文件菜单并选择无效缓存,然后 Intellij 将重新启动并重新生成缓存和其他所需的文件,一切都会好起来的!
回答by dmarges
It seems like your layout doesn't have anything in it. If you expand your res/layout directory in your project, double click on the main.xml file. Just add a button or something and give it an id. That should worl
看起来您的布局中没有任何内容。如果在项目中展开 res/layout 目录,请双击 main.xml 文件。只需添加一个按钮或其他东西并给它一个 id。那应该是
回答by Mohd Saleem
This problem is come if Your Project java complier is differents than your android java compiler.Change the Project Setting and clean project is working. if problem is not solving then goto project properties->android -> select any android version then compile
如果您的项目 java 编译器与您的 android java 编译器不同,则会出现此问题。更改项目设置并清理项目正在运行。如果问题没有解决,则转到项目属性-> android -> 选择任何 android 版本然后编译
回答by taimoor minhas
Make lines of code in comments where you are using "R.id" and then run your project.
在您使用“R.id”的注释中制作代码行,然后运行您的项目。
then for next time remove comments and run it again.
然后下次删除评论并再次运行它。
Hopefully it will work
希望它会起作用