Android R file constants not auto-updating - android

I've searched high and low for people with the same question but have only found some variations (i.e. R file not generated at all) that don't help. I'm VERY new to Android development (day one, actually) but from some tutorials I've been reading, this behavior is unexpected. Basically, let's say I create a TextView in my activity's XML file and give it an appropriate android:id. I save the file (and all other files), but if I try to use findViewById in my Java file, the R.id.[view name] is not listed (and displays an error if I manually type it in). If I do a "Build Project," it works, but to have to run the Build command upon creation of every new component will be a huge burden! Has anyone else experienced this or have ideas on why it's happening? Thanks in advance!

If everything else you mentioned is correct, then the only way for you to NOT get the proper R variables is that you might be importing "android.R;" file in stead of your actual R file "your.package.R" which contains the variables you are looking for.
Regards!

You may have to tell your IDE to automatically rebuild changes you make. For example, in Eclipse this would be in: Window > Preferences > General > Workspace. Make sure Build automatically is checked.

Related

Not able to create string resources in a android module

I have made a simple Android library for my personal use. Now I have to add some common string resources in the library module so that I can access them in the project wherever I use this library instead of creating the same string resources every time in every project I make.
But when I am trying to add a new string resource Android Studio is giving me an error saying:
"msg_example" is translated here but not found in default locale
I am not able to figure this out. Please help.
There is one similar question posted. But that does not resolves my issue. The answer says:
copy auth_strings.xml from values-en-rIN (locale values for INDIA you can use any locale) and paste it in default values folder.
According to the answer above, the person assumes that the developer is having a locale values directory like values-en-rIN and the string resources in it are getting the error. So he is asking to copy those string resources from that locale values directory to the default values directory which ultimately resolves the issue according to him.
But I don't have any other values directory than the default one. You can have a look in the following image:
So creating the string resources in it is giving me the error. My question is very simple and I have not done anything extra / wrong in the project which is giving me the error.
Hope you might have got the issue / difference between these two project scenarios.
Since you don't have any other values folder than the default folder then I think it's not your fault with the error message you're facing, so I guess it's a problem with Android Studio. You can fix it by invalidating the cache of Android Studio and restarting it. You can do this from the main menu:
File -> Invalidate Cache / Restart... -> Invalidate and Restart
Happy Coding!

Why there is two r files under r directory in android studio?

I am beginner in Android app development.I have a doubt in R files.I can see two r files under r directory in android studio.What is the significance of both files.I believe one R file under my specific project folder is to display the layout.What is the need of the other one. Please help me to clear this query.
The one under android.support.v7.appcompat hold "resources" (I am using this word very very loosely) for appCompatv7. This is important because in the future, you may find yourself needing to make your app compatible for older versions, hence, an "appCompat"ibility resource is needed.
The one under com.example.apzalbahin.basiccheck is where the IDs of your views (TextViews, EditTexts, etc) are filed in.
Normally, you don't touch the R files since they would cause major problems for your application. This was a common mistake back when Android Studio wasn't out yet and Eclipse had to be used -- people accidentally modifying or deleting the R.java file.
Just leave it all on it's own and you won't have problems.
There are two configuration mode: Debug and Release ... so two R.java files.. however both have the same codes i guess.

Android studio does not create files

I added a new fragment to my project in Android studio like the way I did it a couple of times before, but now it says that this file das not exist (and is read only). I cannot edit a thing in it but also do not know how to correctly close it, so I can't use the name, which I really want...
I cannot find the file anywhere within my project and it is also not listed in the project+path (Name/App/src/main/...) but in the entire windows location (E:/...).
And how can I create a permanent file, or better, why does it not work any more?
This must be a fault on your side. Are you sure you are in the right folder? This is the only explanation I could think of. So you creat the files but outside of your project's folder, so you cannnot edit them or anything else, but read them...
Bene
Make sure that you have no LINT ERRORS.
For checking it:
Go to the bottom right side of Android Studio. Tap on "Gradle Console" tab.
If you have some errors(red text) - remove them and try again.

Newly created layout files not added to R.java

I have tried literally everything to figure out why no new layout files are being added to my R.java file in eclipse. Nothing is working.
I have:
Restarted Eclipse
Used Project>Clean
Deleted R.java (had to restore because it would not autoregenerate no matter what I did)
Ensured all methods are defined in SDK version
Rebuilt the project
Unchecked auto-build
Every combination of above options
Been tempted to throw my computer across the room
Does anyone have any suggestions?
Check you don't have any errors inside one of your xml files. That is, check not only for xml file name errors but also for errors in the xml code, for example an illegal attribute, a forgotten tag etc. Check this in all your xml files, not just layout files but also anim, xml drawables etc. These errors don't show up in the error log but will prevent the R.java from being generated.
When I experienced the same issue as you this was often the reason. The error highlight was only visible inside the file, not the error log.
Net, go through every xml file with a fine comb.
I faced this kind of problem many times. From my thoughts it is caused due to the problem of images in the drawable or case sensitive of layout file (everything should be small). Check your case sensitive of layout file.
I will give all possible solutions that i had done when something like this happened with me
Project-->clean
Check for library if there was any for that
RightClick-->Properties-->Android-->add
Restart Eclipse
Check in
the manifest for if you have many packages make sure u are using the
activity declared package.
I would check the imported R. The problem could be that you have
import android.R;
instead of
import com.myproject.R;
Project -> and check Build Automatically
Is there any error in your resource files? If there is any error in your resource files the R file will not be generated.
When first of u go on Project->Clean the your application then refreshing and auto genrated R.java file for your apps.
Not to be repetitive, but did you check the Android manifest file? The error there doesn't usually pop up.
Did you check for other locale letters (maybe whitespaces too?) in the filenames? Letters that look exactly like Latin.
I think I had similar problem with the names. You can try rewriting your files' names.
Project Explorer (Right click on your project) -> Build Project
This worked for me.

R.java file not updating with UI element's id

I added some UI elements to the main.xml file in the res\layout folder and when I try to access them through the call, R.layout.my_uielement, the UI element that I created is not there. Even when I add a new xml file with UI elements, it still doesn't show up in the R.layout class. I have made checked the ids on them and they have the correct format (I think): android:id="#+id/my_button". What could be the problem? Do I need to compile the code first?
UPDATE:
I have already tried adb kill-server then adb start-server. It doesn't seem to help. I have also tried R.id.my_uielement, it doesn't register either.
This may not be the case for you, but sometimes when I use something from the android.R package (default android resources) and I use it in my class before making reference to anything from my own resource package, Eclipse adds the import android.R; statement in my imports. This will conflict with the default behavior of implicitly looking in your package's resources.
Check your import statements for import Android.R;. If it's there, remove it. And, if you're using anything from android.R, fully qualify it when you use it in code.
Clean project works fine. In Eclipse if you just change some xml components, it doesn't always generate a new R file. Cleaning does it.
Use R.id.your_ui_element, not R.layout.your_element
I faced this problem too. The R.java was neither generated nor updated.
If R.java is not generated when you create the project, run your 'empty` project. An R.java with references to resources will be automatically generated.
If R.java is not getting updated, Check for updates in Help -> Check for Updates
in Eclipse and update your SDK and ADT if required.
After 2 days I found a way to fix it...
I had saved half done project in my mail, so when R.java vanished i just replaced it with the old R.java file, this fixed a lot of errors, then made necessary changes and built the project(build it not clean)...I do not know whether clean and build performs the same job or not..but everytime i cleaned it , R.java used to go invisible...
Clean is working because it BUILD the project after cleaning. So the problem is ( at least it was in my case, because I switched off the Automatic Build a few days earlier ) that after you add an item or anything in a .xml file, the eclipse doesn't build it and can't refresh the R.java .
I've noticed that my R.java file is not always being properly updated when I add items to my strings.xml file or elsewhere. If I use a new value I added like R.string.newString the actual integer value might still be pointing to R.string.oldString or whatever (it seems to usually be the element directly above or below the new element in strings.xml but I haven't nailed down the pattern for sure yet).
You're not supposed to modify the R.java file, but it doesn't say anything about deleting it (not that I've seen, anyway). I've found that deleting R.java usually fixes my resource-related problems because Eclipse immediately regenerates the file with the integer values properly assigned.
Please note, I'm very much a beginner at Eclipse so I may not understand the consequences of deleting R.java but it seems to be working well for me so far. If what I'm doing is very bad, please let me know.
None of these worked for me!
The only way i can access an #+id/foo is by myPackage.R.id.foo.

Categories

Resources