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!
Related
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.
I update my SDK, and now it is giving error in my whole project. Says that there is a class named R, which is where the layout call.
this with error in the line
import br.com.projeto_tcc.R;
R is the automatically generated with references to all your resources. It is used very frequently in android, so when it 'disappears', it seems to cause an error throughout the whole project.
As has been mentioned in comments above, a rebuild will sometimes solve the issue. However, if the issue is still persisting and the compiler is still unable to find the class 'R', then you need to start looking at your xml files (layouts, menus, etc).
When there is a syntax error in an android XML file, the error notification is not displayed as prominently as it is for errors in Java files (if you are using Eclipse with ADT plugin, as most android devs do). As such, it is pretty easy to miss a typo or other syntax error in one of these files unless you go through and look in each individual one (or check the errors tab in the window at the bottom of your screen if using eclipse/adt).
Syntax errors in these XML files prevent the generation of the R class; therefore, when the java compiler is looking at the references you make to the R class (for example, R.layout.foo or R.id.bar), it cannot find the class, and highlights those lines of code as errors.
So, make sure that your XML files are all well-formed, because this is a fairly common issue, especially if you are just learning your way around adt.
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.
So I'm trying to make myself a little alarm clock app to learn android. Just to do some things that I've always wanted in an alarm clock, and have it be my own. Motivations aside, after coding the interface and a bunch of other functions, I've decided to borrow a bunch of the code, if not most, of the android alarm clock source.
I've already brought in all the res stuff, and I've included all the src files under their original com.android.alarmclock namespace so that they can still reference each other properly. However it refuses to generate the R file for the new files.
When I clean and rebuild and all that, it still generates a build file and an R file for the stuff in my package (com.nathantempelman.alarmapp) but I still get an error on all the android AlarmClock files.
Should I be changing the AlarmClock files so that they reference the R from my package somehow? Or did I miss something when I updated the AndroidManifest file that is stopping another R from building or something?
Or is it something else entirely? Is it a stupid idea to try to drag another application's source into mine? Should I open it up in an external project and try to reference it somehow?
I'd love to hear some ideas, if anyone has had a similar situation.
Thanks for reading.
That is not how it's done. You want your Alarmapp to extend ALarmClock or extend the ALarmCLock classes or interfacces you want or need.
You can't drag source code in like that without re-factoring and tieing up all the loose ends, hence alarmApp extends AlarmClock.
Take a look here Hope this helps
I tried the library method, but that didn't work. In the end, I ended up just dropping the smaller project into the bigger project, combining the strings xml and android manifest, changing the intent classes of the activities in the android manifest, and dragging all the relevant pictures in. Quite a bit of work in the end, but it can be done.
Quick checklist:
Splice android manifest files
- if the package names are different, make sure the package name at the top of your manifest only extends as far as both packages are the same
Make sure all the resources make it over
Splice the strings.xml, styles.xml, and anything else that is common
between the two projects
Thought I'd post this in case someone else tries to do the same thing eventually. Best of luck
I am trying to develop an app to supplement the built-in music player. I've used git to download the com.android.music package and looked around at its code. I can launch the music player by copying some of its code and launching activities with intents.
Now what I need to do is get a handle to its current view. In the MusicUtils.java file, I see a line that says
View nowPlayingView = a.findViewById(R.id.nowplaying);
I'd like to do the same thing. Only I don't have access to the R.java file, so I can't write e.g. R.id.nowplaying. How do I do this? How do I reference the music players R.java? I do know the R.java stuff is declared public so that shouldn't be a problem. Right?
Is it even possible? I saw this related question and am now wondering: How to load com.android.music code into Eclipse and compile?
Btw, I'm working against the Android 2.2 SDK, but it'd be helpful to know if the answer is different for older versions such as 1.5. Thanks.
You cannot get a handle on a View that is not in your process, even if you have access to the R class.
The R class is code generated by the Android plateform from the XML's resources files found in the /res directory of the project. It means you want to use the git repo where you got the MusicUtils.java and go to the root dir of the related project to look out in /res/layout/. You then have to find an XML layout file declaring the View having the id "nowplaying".
Copy this file into your own project 'res/layout' directory, and Android will compile a R class for you to access the R.id.nowplaying you miss so much.
You have the source code, so take the resources and build your own R file. Also, you can access android system resources by android.R.<type>.<name> as well.