So I was happily coding away, and suddenly an error has appeared in my R.java class.
It's created a string, and says there is a "syntax error, and that I should delete the token 'sword'". I tried fixing it, but have instead created a duplicate string, "Long".
Here's the code:
public static final class string {
public static final int Long=0x7f050001;
public static final int Long Sword=0x7f050002;
public static final int app_name=0x7f050000;
public static final int desc=0x7f050003;
}
Does anyone know how to fix this error? Also, R.java doesn't like to be edited, so how to you save manual changes?
You named a field in your strings.xml called "Long Sword", change it to "Long_Sword", spaces aren't supported in the names.
Also, never ever touch the R.java file
public static final int Long Sword=0x7f050002;
Is not a valid field name.
Naming Conventions: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
You need to remove static from string class. A top level class cannot be static
public static final int Long Sword=0x7f050002; // not valid
Related
Why is it giving me "cannot resolve symbol raw" when i try to access the folder with the following code Field[]fields=R.raw.class.getFields();
The same code works when i add any file to the raw folder. The folder is in "res" directory.
Note: I have tried using the complete package name also.
When you create an Android resource folder such as layout, drawable, mipmap, etc.. The android will create a file named R.java. You can find it in
app\build\generated\source\r\degug\your\app\package\name\R.java.
It basically a normal java file which has been generated by android automatically for referring resources by ids in our code.
R.java
public final class R {
public static final class layout{
public static final int main_activity=0x7f0a0000;
public static final int login_activity=0x7f0a0001;
}
public static final class drawable{
public static final int ic_add=0x7f0a0000;
public static final int ic_edit=0x7f0a0001;
}
public static final class mimap{
public static final int ic_launcher=0x7f0b0000;
}
}
From Android official site:
Resources are the additional files and static content that your code
uses, such as bitmaps, layout definitions, user interface strings,
animation instructions, and more.
It means res folder is read-only and you cannot modify (add, update, delete) the content side.
Back to your problem, when you create raw folder but do not put any files inside and android know that in the runtime you cannot modify this folder as well, so why we need to create a refer to the raw resources anymore. It does not make sense. Because there is no raw class in R.java so your compiler display the error.
cannot resolve symbol raw
But if you create a file and put into raw folder, then after build process, a class raw will be created in R.java file.
public final class R {
public static final class layout{
public static final int main_activity=0x7f0a0000;
public static final int login_activity=0x7f0a0001;
}
public static final class drawable{
public static final int ic_add=0x7f0a0000;
public static final int ic_edit=0x7f0a0001;
}
public static final class mimap{
public static final int ic_launcher=0x7f0b0000;
}
public static final class raw {
public static final int sound=0x7f0b0000;
}
}
And now the error gone.
I think the problem is you are using wrong package of R.
Make sure R should be from your package name rather then some other android Native class.
Could you please tell me what three single components of this name refer to?
R.id.myView
R - R.java is an auto-generated file by aapt (Android Asset Packaging Tool) which contains resource IDs for all the resources of res directory.
public final class R
extends Object
.id - Find view using its id "defined by you"
public static final class R.id
extends Object
myView - It is the view that you defined using the android:id="#+id/your_view" attribute in your XML file.
So, finally we can find or identify any view using R.id.your_view.
android.R -R is a final public class in android. It extends Object class and it has many nested classes like R.id, R.anim, etc.
R.java is Automatically System generated file and contains the id of each resources used in the Application which is used to make reference. R.class contains IDs for all your android resources.
android.R.id is a nested class of R class. It has many static final constants like text1, toggle, button, etc.
android.R.id.myView is an identifier of a View class. It represents an id for corresponding view defined in XML.
Android R.java is an auto-generated file by aapt (Android Asset Packaging Tool) that containsresource IDs for all the resources of res/ directory.
Whenever you use any recourse in you project then its one Unique Id will be generated automatically and you can identify that resource by using that id. You can not delete this file.
R :- Java class is collection of your all resources with its related id.
id :- Whenever you create any resource and assign the id by using #+id then R file create one unique id for that resource.
myView :- Its view id name that user can identify and By using that view id name we can identify that view in activity java file.
Below are the example of view id's in R.java file. If you want to show that where it is located then check this answer.
Example of R.Java file
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int menu_settings=0x7f070000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int activity_main=0x7f060000;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int hello_world=0x7f040001;
public static final int menu_settings=0x7f040002;
}
public static final class style {
public static final int AppBaseTheme=0x7f050000;
public static final int AppTheme=0x7f050001;
}
}
This is the error, which is generating again and again.
I've cleaned my project.
public static final class id {
public static final int =0x7f07005b;
public static final int button1=0x7f070004;
public static final int button2=0x7f070005;
public static final int frameLayout1hardMatchup=0x7f070009;
There is no error in String.xml, here is my String.xml.
<string name="hello">Hello World, MemoryCardsActivity!</string>
<string name="app_name">Princess Memory Cards</string>
<string name="text_name">Congrates</string>
<string name="text_help">Turn over pairs of matching cards, tap to flip two cards, as the match happens, they get removed from the screen.Try to get all cards match in minimum moves and proceed to next level</string>
And also there is not any #+id kind of error.
What else could be the reason of this.
Any positive response would be really appreciated.. :(
You are missing a variable name
public static final int = 0x7f07005b;
instead
public static final int variableName = 0x7f07005b;
I produced a similar error by changing one of my layout XML files. I changed
android:id="#+id/image_frame"
to
android:id="#+id/ "
The error I see with this change is
public static final int =0x7f080010;
// Syntax error on token "int", VariableDeclaratorId expected after this token
This seems to be what #codeMagic was describing.
am very much a newbie when it comes to android development so bear with me. Im currently using Windows 8 / Eclipse.
My issue is, the findViewById function seems to have gone mad, although I could very well be using it wrongly.
The app was working fine, then I 'dropped' a few new TextViews on the page which seems to totally confuse it.
The findViewById function now either finds the wrong control completely, or doesnt find anything at all (and will return null). I've checked my activity_main.xml file and the id's are still correct.
Can anyone help?
This is a typical example of my usage:
public void toggleButtonNetwork_Click(View v) {
ToggleButton tb = (ToggleButton) this.findViewById(R.id.toggleButtonNetwork);//did work, now does not work!
}
The only insight I may add is that my R.java file looked like this when it was working:
...
public static final class id {
public static final int menu_settings=0x7f070004;
public static final int textViewGPS=0x7f070003;
public static final int textViewNetwork=0x7f070001;
public static final int toggleButtonGPS=0x7f070002;
public static final int toggleButtonNetwork=0x7f070000;
}
...
and now looks like this (broken):
public static final class id {
public static final int menu_settings=0x7f070006;
public static final int textView1=0x7f070004;
public static final int textView2=0x7f070005;
public static final int textViewGPS=0x7f070002;
public static final int textViewNetwork=0x7f070003;
public static final int toggleButtonGPS=0x7f070000;
public static final int toggleButtonNetwork=0x7f070001;
}
This is a quite common problem. Try and invoke the Project/Clean... on your project. It sometimes happens that the automatic generating of the R classes goes wrong and this will force them to be rebuilt.
Everthing is fine.
Always Just go to project -> clean
And than run .
Thats it.
This doesn't answer your question. I suspect that A--Cs comment is correct.
But, there is nothing to be confused about. findViewById is really simple.
When you compile your app, the compiler generates R.java and adds a line for each view in your layout XMLs (and strings, drawables, colours etc - anything that is a "resource") and gives it a unique ID.
public static final int toggleButtonNetwork=0x7f070001;
The ID will change as you change your resources, but it doesn't matter. When you use findViewById, you give it the "friendly name", in this case R.id.toggleButtonNetwork, which is compiled into 0x7f070001 since public static final int toggleButtonNetwork is a static constant.
When you inflate your view from XML, typically with setContentView, the view hierarchy of objects is built. The id of each object is the id found in R.java.
findViewById returns a reference to the object, of type View, which you then cast to whatever type of View it is which is why you use
= (Button)findViewById(R.id.toggleButtonNetwork);
Strangely I'm getting really strange resource ids when R regenerates, that looks like:
public static final int #+id/#+id/music_thumb_img=0x7f060063;
public static final int #+id/music_main_text=0x7f060064;
When they should look like:
public static final int music_thumb_img=0x7f06001d;
public static final int music_main_text=0x7f06001e;
Needless to say, this R.java doesn't compile, but there doesn't seem like there's any problem in the java code nor the .xmls, at least not obvious ones that IDEs can pick up.
Has anyone encountered this?