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;
Related
I almost completed my project and now kind of stuck at the place where I need to import few java classes to MainActivity from other directories in android studio.
I have made 3 packages named app, activity and helper that has got 2 java classes each, but now in src/main/java/MainActivity, I am unable to import them..
I am attaching a screenshot here for you guys, please help.
FYI - I went through few discussions made earlier on kind of similar topic but was unable to resolve my issue, try to be specific, n m sure you will be.
You have to create different packages inside java folder and than you will be able to import class from different packages.
Ex:
Inside java - create one package, again right click on java and create another package.
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.
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.
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 have developed some reusable android component which is basically a class . This class has some resource dependencies e.g. some png drawables, some xml layouts etc. So this class referenced the auto-generated R file.I would like to distribute this code in a single package like jar file to other developers for use in their applications.
I have read that the only possible solution is to distribute code together with all my resources, which others have to copy to their "res" folder (source).
So I created a jar file having the class file (say MyClass which is in the package com.xyz.android.app) and resources and tried to use this in my new application.
So I added the jar file to my new applications build path using add external jars option in eclipse and copied all the resources to my new application's res folder. (The activity class say MainActivity of my new application is in com.abc.myapplication package, just for the case if it may helpful)
But when I run this new application there is java.lang.ClassCastException in the MyClass class. I tried to debug the application and then I found that in the MyClass class, there is "R cannot be resolved" problem.
Then I changed MainActivity's package to com.xyz.android.app (which is not the way, other developers will be happy to do), But again the same problem.
But When I just copy the source java file such that both MainActivity.java and MyClass.java are in com.xyz.android.app package then application runs fine.
So if I need to distribute such that other users need not to bother these package naming things,
how can I accomplish this? Please help !!
Edit
In android.jar, there are also some resources. How are they referenced in a project? How are they used in android classes? There is also android.R file?
Is it not possible to do the same thing i.e. to make jar file like android.jar for my reusable code?
As Nic Strong noted, there is nothing much built into the project to manage this, and current indications are that the upcoming changes to the SDK tools may only help a bit.
I am organizing some other tools to help deal with this problem. I hope to have developer documentation published in a few days.
This is not so easy to do at the moment. The problem is the project using your jar does not know to look in there for drawables etc.
The good news is it should soon be possible (hopefully with SDK 2.2 which is rumoured to be released at IO next week). See this blog post http://mobilebytes.wordpress.com/2010/04/27/android-tools-version6-coming/
I've been playing with Mark Murphy's ParcelHelper class at http://github.com/commonsguy/cwac-parcel. In a nutshell, it's a collection of methods that let you access the components of 'R' by name without needing to import 'R' into your code.
If I understand your question right, this is exactly what you want. You may still need to copy some resources, such as styleables, into your project.