R file isn't creating new Ids' codes - android

Firstly, I have to say that I am just a beginner with the Android programming, and I may not understood things correctly. ^^
Secondly, my problem is that the R file (the one which create the different IDs of the objects) just stopped to create new ones.
I noticed that when I created a new image_button at the main_layout and when I tried to look for it on the MainActivity it wrote that it didn't exist.
Moreover, only after I modify the R file (which I should not touch according to the system warning), I have control over the new IDs.
And after breaking my head for two days - I decided to ask you,
The R file:
public final class R {
public static final class attr {
}
public static final class dimen {
/* Default screen margins, per the Android Design guidelines.
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
*/
public static final int activity_horizontal_margin = 0x7F040000;
public static final int activity_vertical_margin = 0x7F040001;
}
public static final class drawable {
public static final int ic_launcher = 0x7F020000;
public static final int loading_i = 0x7F020001;
}
public static final class id {
public static final int action_settings = 0x7F080004;
public static final int button1 = 0x7F080001;
public static final int imbt = 0x7F080003;
public static final int textView1 = 0x7F080000;
public static final int tv1 = 0x7F080002;
}
public static final class layout {
public static final int activity_main = 0x7F030000;
public static final int firstpage = 0x7F030001;
public static final int loading_screen = 0x7F030002;
}
public static final class menu {
public static final int main = 0x7F070000;
}
public static final class string {
public static final int action_settings = 0x7F050001;
public static final int app_name = 0x7F050000;
public static final int hello_world = 0x7F050002;
}
The creation of the new object:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imgBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

Yep, you shouldn't edit the R file directly. The problem you meet is normal. It should be causes by a file out of sync.
Hopefully, you could fix it by:
Option #1: Try to refresh whole project and 'Project -> build project'.
Option #2: If #1 doesn't work, hit 'Project - clean...', and then rebuild the project.
Option #3: Sometimes, #1 and #2 don't work. The last ultimate way,
which always works for me, is to delete the `gen` folder and rebuild the project.

Make sure that
Project --> Build Automatically
If it's already checkedm, run clean:
Project --> clean
If that doesn't work, then most likely there is an error in one of the files, most likely one of the layout XML files. Check the Problems view:
Window --> Show View --> Problems.
Fix all the problems (you can ignore the warnings).
Your last resort is to delete the R file (backup first) and then re-built (or clean).

It's one of the most common problems beginners face; even I faced this too. :)
The most obvious solution is to try cleaning and building your project. Sometimes it works, and most of the times it doesn't.
When cleaning and building doesn't work, then most likely you have some error in your code and that too in XML files. Android tools sometimes don't show which XML file has the error, so you manually need to open each file and check for errors. Once you have fixed the errors, your R.java will build automatically.
I hope this helps.. :)
Update:
This used to happen for Eclipse and ADT plugin. Android Studio is the savior. Start using it if you aren't already.

Clean your project and run it:
Project---> Clean then run
See Stack Overflow question Developing for Android in Eclipse: R.java not generating. It gives the perfect solution for you...
Change the build target version and clean the project. After completing that, change that build version to previous. It works for me.

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.

Tons of items in R.java created by android studio

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.

Changing id of string in Android / Eclipse

I start out with the example Android Drawer Fragment App.
I go to strings.xml and rename the id of the string "action_example" to "action_additem".
I go to the classes and change all mentions of this string to the new id.
I press Ctrl+Shift+S to save all files.
Still, I get this error in the fragment:
if (item.getItemId() == R.id.action_additem)...
action_additem cannot be resolved or is not a field
In R.java, I see the string ID was not updated.
What do I need to do to update it?
In the R file, I see two entries for this string:
public static final class id {
public static final int action_example=0x7f080005;
...
}
public static final class string {
public static final int action_additem=0x7f050006;
...
}
Why was only one of them updated and how do I update it to the correct id?
EDIT:
Okay - this is solved. One id was for the action, one for the string.
In Eclipse click Project > Clean...
This should rebuild the R.java file.

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

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