An excerpt from the documentation :
Eclipse sometimes likes to add an import android.R statement at the
top of your files that use resources, especially when you ask eclipse
to sort or otherwise manage imports. This will cause your make to
break. Look out for these erroneous import statements and delete them.
My question : WHY? Why does eclipse keep on doing this?
I have been developing Android applications using Eclipse for a quite some time now but I have never been able to understand why eclipse does such a thing.
When I use Ctrl + Shift + O to organize my import statements, import android.R gets added automatically. And all of a sudden my correct code is suddenly covered in red errors, saying that R cannot be resolved. It can get really scary for a beginner as he has no idea what he did wrong.
In another scenario, suppose there is something wrong with my layout files and R.java is not being generated, it says that R cannot be resolved, as R.java has not been generated due to the errors. As I move my cursor to any of the errors, it suggests me to import android.R.
After working on Android for quite sometime now, I know that never to import android.R, but what I have never been able to understand why eclipse keeps on suggesting it, as frankly speaking, adding import android.R never solved any problem of mine. It just added to the existing problems, which used to be really painful during initial days of development.
So, does anyone know the reason behind eclipse making the suggestion to make an incorrect import? Is it just a bug? I don't think it's a bug, as it would have got fixed at least after it was mentioned on the Android documentation.
If it's not a bug, then what is a real purpose of android.R? What does it exactly refer to?
Your opinions/experiences will be really helpful!
Thanks!
This is not a bug. There are a few instances where android.R can be helpful and solve problems.
android.R is an R.java file like the one you have in your own projects. The one in your projects (your.packagename.R) holds references to the resources you have under your /res folder like layouts, drawables, XML files, raw files, strings etc.
On the other hand, the android.R file holds references to certain default resources that Android has inbuilt, like simple_list_item_1.
Eclipse suggests this and auto imports this sometimes as if your project's R file hasn't been generated due to an XML error or something, your code will be referencing a file that doesn't exist. By importing android.R, eclipse makes sure your code references a class that exists. However, android.R is unlikely to have the same resources you did, and this will raise another set of errors.
Eclipse will also always try to automatically import android.R in your class if you rename your package name via android tools and not rename base package name in your code with the same name because he will assume you have two R's in your file.
Try this...
Window -> Prefs -> Java -> Editor -> Save Actions
Uncheck "Organize Imports." Hopefully that will do it.
After this setting, it works for me.
Related
When I create a new android project (following these steps: http://developer.android.com/training/basics/firstapp/creating-project.html) I automatically get syntax errors and I don't get it. I have attached a photo of the problem. Please help. I downloaded everything I needed to and in the older versions of this, everything worked fine.
You are importing the wrong R. You should be doing
import com.chatnexttofriends.R;
instead of
import com.android.R;
This could have happened if you used the Organize Imports option before the automatically generated files where created, or because there was an error in your resources that prevented the automatically generated files from being created.
If you intend to use resources from both Rs, then you will have to fully qualify one of them. I would suggest qualifying Android's R. I.e.:
setContentView(android.R.layout.activity_list_item);
Although I doubt you want to set the content of your Activity to that of an item representing an Activity in a ListView.
When developing Android applications in Eclipse often you will get errors asociated with the R file, like
R cannot be resolved to a variable
or
id cannot be resolved or is not a field
I am posting this question+answer to try to provide a complete and general look at the common mistakes an android beginner might encounter. If anyone thinks they can provide a better overview, or would like to add, edit or comment, feel free to do so.
There are basicaly 4 different things that could be wrong.
there is an error in one of your resource xml files. This should be indicated either in Problems or in LintWarnings.
a specific, but very common error, is that one of your resource files contains an improper character, namely capital letters.
you are importing R.android, which is a collection of android's own identifiers, not identifiers of your own resources.
If any of these are the cause of your problems. Do NOT import the R file. it's located in the same package as the root of your project.
However, another cause of the problem might be, that you have source code in different packages in your project.
In that case you DO need to import mypackage.R or mypackage.*. Just like you would import any other class from a different package.
Remove this import in your project
import android.R;
I know this is a pretty common question and I looked around the web and this forum for an answer but none of them seem to be working for me. I did the typical stuff like deleted my R.java and cleaned my project, made sure my class did not have a import for the R.java class. I tried rebuilding my project etc.
So here is what I have going on. I am trying to inflate a database from some raw SQL statements. I am using the book The Busy Coders Guide to Advanced Android Development book as a guide to do this.
it gives the following line of code:
InputStream stream=ctxt.getResources().openRawResource(R.raw.packaged_db);
and says the file is located within the res/raw directory like so "res/raw/packaged_db.txt"
I have placed my sql dump file: res/raw/raw_game_data.sql
and here is my line of code that is throwing the error:
InputStream inputStream = context.getResources().openRawResource(R.raw.raw_game_data);
any ideas or suggestions on what I am missing?
Thanks,
Generally, only one R.java is built for your entire project, and it is built in only one place, with one package - the package of your app, declared in your manifest. If that package is com.example.trek, and you refer to R in a class in com.example.trek.tribbles - or any other package - you will need to explicitly import com.example.trek.R
You list off some corrective measures without indicating that you understood the actual problem they were intended to correct, or if you had that problem. I'm not unsympathetic to this attitude in the context of Eclipse, but you can waste a lot of time this way. If you understand Java, then you only need to learn aapt to be able to drop down to the command line and attempt the process yourself.
That said, developers.android.com seem to have removed the documentation for aapt. ...well, don't worry, I'm sure you'll never have problems with Eclipse!
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.
Eclipse is complaining about my Android project saying all my references to resources are unresolved, even though R.java does exist. For example:
signin_btn=(Button)findViewById(R.id.signin_btn); signin_btn.setOnClickListener(this);
I definately have signin_btn defined in my resources. Dont know why Eclipse is not seeing my R class anymore. How to fix?
The thing that happens to me once in a while is that Eclipse decides to "help" me by adding the following statement to the top of the source file:
import android.R;
This means that all non-absolute references will now be matched against the Android built-in resources, instead of your own. Something like "R.id.layout" is now supposed to be in "android.R.id.layout" and not in "com.mydomain.myproject.R.id.layout". Just remove the line.
It happens to me, this is what I think happens:
Whatever process generates R.java starts at the top of your resources and works to the bottom. If it encounters an error somewhere, it just stops... leaving whatever resources haven't been added to R.java out. This is why some of them will say "unresolved", and others will be fine.
It seems that every time you save a file in your project, it regenerates R.java (maybe it's just the files in res/), so you probably fixed whatever the generator broke on, and when you saved AndroidManifest.xml, it regenerated R.java.