Tons of items in R.java created by android studio - android

It's my first use of Android studio. But when I use it to create my first project, I found that in R.java(The location is app/build/source/r/debug/com.example.myapplication)
The beginning is like this:
public final class R {
public static final class anim {
public static final int abc_fade_in = 0x7f040000;
public static final int abc_fade_out = 0x7f040001;
public static final int abc_slide_in_bottom = 0x7f040002;
public static final int abc_slide_in_top = 0x7f040003;
public static final int abc_slide_out_bottom = 0x7f040004;
public static final int abc_slide_out_top = 0x7f040005;
}
public static final class attr {
public static final int actionBarDivider = 0x7f01005a;
public static final int actionBarItemBackground = 0x7f01005b;
public static final int actionBarPopupTheme = 0x7f010054;
public static final int actionBarSize = 0x7f010059;
public static final int actionBarSplitStyle = 0x7f010056;
public static final int actionBarStyle = 0x7f010055;
public static final int actionBarTabBarStyle = 0x7f010050;
public static final int actionBarTabStyle = 0x7f01004f;
public static final int actionBarTabTextStyle = 0x7f010051;
public static final int actionBarTheme = 0x7f010057;
public static final int actionBarWidgetTheme = 0x7f010058;
public static final int actionButtonStyle = 0x7f010072;
public static final int actionDropDownStyle = 0x7f01006d;
public static final int actionLayout = 0x7f01002c;
public static final int actionMenuTextAppearance = 0x7f01005c;
public static final int actionMenuTextColor = 0x7f01005d;
public static final int actionModeBackground = 0x7f010060;
public static final int actionModeCloseButtonStyle = 0x7f01005f;
public static final int actionModeCloseDrawable = 0x7f010062;
It list so many items that if I put them to Word, they will fill 38 pages in 11 font size. I tried deleted them, rebuilt them, restart, create another project, but they still exist. I had used Eclipse so I know how it should looks like. What's the problem? Is it caused by Gradle or Android Studio itself?

Deleting, rebuilding, restarting are not the correct approaches you should take.
These lines are generated by the Android Support Library.
Including the Support Libraries in your Android project is considered a best practice for application developers, depending on
the range of platform versions your app is targeting and the APIs that
it uses. Using the features the libraries provide can help you improve
the look of your application, increase performance and broaden the
reach of your application to more users. If you use the Android code
template tools, you will notice that all the Android application
templates include one or more of the Support Libraries by default.
[1] http://developer.android.com/tools/support-library/index.html
Yet, I don't understand why you want to take them off.

R.java is an auto-genrated file by android AAPT(Android Asset Packaging Tool). It has some significance, for every resource in the res folder a corresponding static integer field is created automatically.
So irrespective of the number of times you delete it, it will be generated again.
If you want to reduce the number of lines in the R.java file try deleting some values in the res folder.
The more layout files, widgets and images and other resources you have, the length of the R.java file is going to increase.
I hope this answers your doubts.

Related

Cannot resolve symbol raw when the folder is empty

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.

Error in R.java class

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

Newbie confused by Android's findViewById() functions?

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);

How to organize Android dialog IDs?

I have a base class ActivityA that has some dialogs:
public static final int SOME_DIALOG_ID = 1;
public static final int OTHER_DIALOG_ID = 2;
ActivityB inherits ActivityA and comes with its own dialogs:
public static final int MY_DIALOG_ID = 1;
public static final int CONFLICT_DIALOG_ID = 2;
As you can see, the dialog IDs are no longer unique within the activity. This is a problem that I keep running into. Easy to fix, but tedious to maintian. There must be some good design pattern for this particular issue. Please, enlighten me!
Q: What is the best way to organize dialog IDs?
In my situation, I create class called AppConstant.java that contain all constant in current application, some of it:
public static final int INTENT_REQUEST_CODE_ACT_MAIN_BROWSE_PICTURE = 1;
public static final int INTENT_REQUEST_CODE_ACT_MAIN_CAMMERA_SNAP = 2;
public static final int INTENT_REQUEST_CODE_ACT_PROCESS_IMAGE_PROCESS = 3;
public static final int INTENT_REQUEST_CODE_ACT_PROCESS_BROWSE_PICTURE_FOR_BACK_DATA = 4;
Using this convention, I can easily maintain my ID within single class.
How about use 1, 2 for ActivityA, then 11, 12 for inherited class, then 101, 102 (or 21, 22) for further inheritance depth.

Android Regenerating R give incorrect resource ids

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?

Categories

Resources