I have the R cannot be resolved error in Eclipse and in NetBeans too
What can I do with it? I made a full new project, but the error开发者_运维知识库 is here: R cannot be resolved.
The error is in the setContentView(R.layout.main);
row.
First :
your error means that the file R.java
is not generated , so you should clean the project ( Project => Clean => check your project and click OK) ,
Second : may be your file R.java is already generated, in this case , check your imports , if you find : import android.R
; change it with your own class R , for example : import com.yourpackage.R;
Hope it helps
Let me guess... you have:
import android.R;
// or nothing
Make sure you have:
import your.package.R;
And of course, make sure you have a file called main.xml
in your res/layout
folder.
There are a few things you can do to resolve it. First make sure that you do not have import android.R in your imports.
The resource might not have been built yet. I find that error often goes away if you build the project.
Right click on the project, click build project, or if you do not have build project available, build all will work too.
The other thing you can do if that doesn't work is to import "R"
import <your package path: for example com.test.R>
Good luck.
Here in NetBeans, I solved this problem by placing the import android.R.*
. The asterisk is important!
If your AndroidManifest.xml claims your package is something different than the Java package your source code is in (which is perfectly legal if managed correctly), you must import R from the package name declared in your manifest.
The problem is that R file is not generated which means that there is an error in some of the project files, most probably in XML layout files. Please check them for errors, and clean your project (in Eclipse Project->Clean) and you should be OK.
(as a side note: you DON'T need to import the R file explicitly, i.e. having import something.R;
(except in the case defined by @mah)).
I had a similar error in Eclipse and fixed it by going to YourProject Properties > Java Build Path > Order and Export and moving the YourProject/src and YourProject/gen to the bottom. My .classpath
file was:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="lib" path="libs/cordova.jar"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
and is now:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="lib" path="libs/cordova.jar"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
Hope this is helpful to someone.
精彩评论