IntelliJ Idea not generate id in R.java

不打扰是莪最后的温柔 提交于 2019-12-03 17:12:00

问题


I recently switched to IntelliJ Idea 12.

Now created a project on android and immediately ran into a problem: not generated id in R.java

Rebuild project does not help.

Maybe in IntelliJ Idea have any subtlety is not known to me?

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$000(MyActivity.java:11)
at com.example.poem_new.MyActivity$1.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:

    /* This stub is for using by IDE only. It is NOT the R class actually packed into APK */
public final class R {
}

回答1:


What you're observing is probably the by-design behaviour in Idea 12:

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."

In your case, look into ~/.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.

It should also be hooked up to Idea's functionalities of autocompletion, usage search etc by some sort of internal Idea magic.

The empty R.java is normal (although misleading, and Jetbrains intends to change this behaviour).




回答2:


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:

  • remove import of android.R
  • delete gen directory

After a clear build gen reappeared and the project was ok.

Hoje it helps somebody.




回答3:


Closing all intellij windows, renaming the project folder and opening it again solved my problem.

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!




回答4:


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




回答5:


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




回答6:


Make lines of code in comments where you are using "R.id" and then run your project.

then for next time remove comments and run it again.

Hopefully it will work



来源:https://stackoverflow.com/questions/13661169/intellij-idea-not-generate-id-in-r-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!