The import com.example.myfirstapp.R can not be resolved - android

I have been working my way through the Tab menu tutorial and have come across multiple errors. After doing some research on here I found the common solution was to change the Import android.R to import com.example.myfirstapp.R.
This is giving me the error that this file cannot be resolved. Can anyone tell me why I can't import this file? I have a hunch from researching on here that eclipse has not built the file, but I have no idea how to build it as I am new to coding as this is just a hunch and may be completely wrong.
package com.example.myfirstapp;
[Edit}
i have removed the "com.example.myfirstapp.R" and have CMD-SIFT-O to restore the imports but i still have the following errors.

You don't need the com.example.myfirstapp.R unless it is in a library. If it is in com.example.myfirstapp, remove it.
Also, trying cleaning your project and rebuilding it.

Make sure you don't have any files, pictures, etc in your drawable or assets folder with capital letters, spaces, or dashes. Just use plane old lowercase. Also, try to clean your project. Go to project > clean. Then restart eclipse. Hope this helps. Also eclipse won't reconize any changes in XML files without saving first. Make sure to save after you add a button, ect.

Related

R cannot be Resolved To a Variable only in one line

My friend created an android application using phonegap, jquery and facebook sdk in eclipse. I wanted to run the application in my computer. So I imported the whole project into eclipse. I downloaded all the necessary APIs. Initially I got a lot of errors, but I solved most of them.
Now I am stuck with just one error. And it is in the src/com.facebook.android./FbDialog.java file. One particular line
Drawable crossDrawable = getContext().getResources().getDrawable(R.drawable.close);
gives the error R cannot be resolved to a variable.
I have gone through a lot of similar questions on this website as well as other websites. I have cleaned the project innumerable times. The R.java file builds successfully. Also, my friend is not getting any error when he runs the same project in his laptop. So I don't think there is any problem with the coding part.
I am definitely missing some important setting or download. Please help. This one error is giving me a lot of headaches.
I am a new user her so cant upload a screenshot.
1st. res/drawable folders (ldpi, mdpi, hdpi) must contain an image named close, if not add it. I doesn't really mater, in order to overcome the error, in which of them the file exist.
But in order to use resourses properly see the Android documentation here
2nd. File FbDialog.java must have an import of the R.java sometimes the R imports get mixed with clean & built and instead of import your.package.com.R; it is included the import android.R;
Depending on the dependency libraries or related projects of main project the close file resource may be a part of an other library in that case you will need import that.package.com.R;. So if it is part of Facebook SDK then you will need import com.facebook.android.R;
I hope you get the logic of how R imports work.
Update: Lets force it to work, change line to:
Drawable crossDrawable = getContext().getResources().getDrawable(your.package.com.R.drawable.close);

android eclipse adding folder to project

i have an android project which has a lot of classes I've built.
i want to organize it to sub folders and my package in eclipse.
when i'm adding a folder and move files over there, what changes should i do in the code so the file which need this classes can reference to it?
is it something in the:
import android.app_name.folder_name.class_name
help will be appreciated, my project is beginning to be a big mass..
if its not possible i will be glad to hear about other solution
Eclipse has a rename/refactor method you can use. The only issue I'm familiar with is the fact that it seems to break the manifest.xml
Yes you can import project into Eclipse...
Then you should create a Package to manage related classes. (FYI, package is ultimately creates folder hierarchy on your drive).
Note: Make sure you provide a correct reference of the particular class in AndroidManifest.xml file.

How to properly change the package name in AndroidManifest.xml (e.g. in project zxing/barcodescanner)

I'm pretty new to android development, so I hope my question is easy, but not completely stupid. I'm using Eclipse to build an android application. It is based on the barcode-scanner of the ingenious guys from zxing. I already did quite some changes to the original code and everything works fine. But I still have the problem, that the original barcode-scanner and my app cannot run simultaneously on one mobile device. As far as I could find out, the problem is the package name. So I tried to change it to something else. But that srew up my entire project, because I can't access my resources anymore (e.g. findViewById(R.id.btDone); <-- R cannot be resolved to a variable).
Can anyone tell me what else I have to change to make my code work again?
This the beginning of my AndroidManifest.xml where I tried to change the package name:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.zxing.client.android"
...
I also found an interesting entry in build.properties: (?!)
application-package=com.google.zxing.client.android
Thanks you guys!
This should do it: Right Click on project -> Android Tools -> Rename Application Package
Android - Package Name convention
The package refers to the file directory you made. If you still have problems, especially with android, sometimes doing project->clean and then rebuilding fixes some of the linking problems with resources
Assuming you choose to go with the new package name:
com.superscanner.android
And with the old package name being (for example):
com.google.zxing.client.android
Go through all the source code and change:
import com.google.zxing.client.android.R;
To:
import com.superscanner.android.R;
You'll also have to rename all your directories to match your new package structure, and change your import and package statements throughout, but this should get you going.

Should I be importing com.myapp.R into my activities?

IntelliJ is moaning if I don't have the following imports in my activities :
import com.jameselsey.apps.romandroid.R;
I've got several other apps which don't require an explicit import of the R file, however this one will not compile if I remove it, I get the following error :
\src\main\java\com\jameselsey\apps\romandroid\activity\RomanToArabicConverterActivity.java:[31,24] package R does not exist
Understandably there are various other questions on SO on how to resolve this (none of which appear to work for me), but my question is : Does it really matter? Is there any reason why I should be wildly concerned here, or just go ahead and import it?
Tripled checked all my config xml, can't find anything that would prevent R being regenerated, mvn clean install still failing...
I would go ahead and import it. I don't believe you should be concerned.
i wouldn't worry about it.
On a side note. If you want to regenerate R, you need to do a clean build after ensuring that there are no errors in the xmls.

R file is missing android

i was using Eclipse Helios but due to performance issues i changed to Eclipse Galileo and installed the ADT plugging, and added my sdk folder to Elcipse Preferences. Now R.java disappeared from all of my projects. How can i fix this, i did Project/Clean but that won't generate the files, there's nothing wrong in my xml and there's no out.xml in any of my projects.
Thanks in advance
In my experience there is a mistake in the xml somewhere. Eclipse will no doubt tell you where. After I fix the error R appears.
Sometimes when you clean your project the R file disappears, I had the same issue.
The way I fixed it was:
-Make sure all the "import android.R" was removed
-Clean again (if this doesn't fix it, restart eclipse and try again)
Or
-Put the pointers to R file in comment f.e. // setContentView(R.layout.main);
-If all the pointers to R file are in comment, you should get only warnings in the file, and hopefully errors somewhere else.
-Fix the errors and then uncomment the pointers. Sometimes eclipse ignores some errors and drops the R file and then says nothing about it, which is annoying, but this will fix it. :)
-If this still doesn't work, you can try create a new project and copy paste your code in it.
I had this same issue today and figured it out. The reason this happens so often when including external/example files is because often times these examples reference layouts in your application, but do not have access to the package and therefore cannot see the R.java file in that package. to make things clear, here's the beginning of the R.java file:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.conceptualsystems.dashboard;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int csc_logo=0x7f020000;
public static final int icon=0x7f020001;
}
public static final class id {
public static final int activation_code=0x7f070012;
public static final int alpha_bar=0x7f07000b;
public static final int alpha_label=0x7f07000a;
notice the package name is whatever the package name of your application is. .java files that are not included in this package (ie, your example code you just dropped in) will need to explicitly reference that package file like this:
package com.example.android.apis.graphics;
import android.app.Dialog;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import com.conceptualsystems.dashboard.R;
the last line is the one to pay attention to. after importing the resources explicitly, the resources will be available in your example code.
created new project in eclipse, then I imported the sample files into it, yet still same error. You might want to try the following:
In Eclipse -> Right Click on Project -> Android Tools -> Fix Project
made sure that all resources are lowercase.
Try unclicking and re-clicking build project automatically in the project dropdown, closing the program each time. There have been know bugs about this for a while but there's not always a set fix.
When it once happened to me it turned out it was because i'd named an .xml file with a capital letter. Worth checking.
I had this problem when upgrading from Eclipse/Helios to Eclipse/Juno.
I set up the IDE and then imported an existing (working) project. If I then ran it in the emulator it ran but in its original version although the java code was for the latest version.
I then did a Clean (as explained in many other posts) and a (re) build but then ended up with host of errors due toSmissing R.java file.
The problem turned out to be some 'errors' in the layout xml files which had not been recognised by Eclipse/Helios. Specifically, in my case, there were some layout_width='match-parent' (and height) settings which Juno objected to. I tried changing those directly in the xml files but Juno wasn't having it and still reported it as a 'match-parent' error (although it had clearly been changed).
The only thing to do was to go to the graphical layout, right-click to get Properties and then change the Width and Height properties there. Also, as they were already showing as 'fill-parent', I had to first change them to 'wrap-content' and then change them back to 'fill-parent'.
Once I'd done that for all instances of 'match-parent' I could then do a build and run the latest version in the emulator.
There are mistakes in res. Once I have the same xml files in layout and menu, generate failed, no R.java generated.
Localize the correct R. package import, should be in your packages.
ex. import com.yourdomain.test.R;
This is where ADT Android project is a bit different from a normal Java project. An android project requires a constantly updated persistent R.java file. Among other things, the R.java file has all the resources IDs provided in your App and maintains interface's consistency.
So first make sure R.java file is being generated for your android project.
To find that out first go to the top of ADT (Eclipse) and clean the project PROJECT->CLEAN
If the 'Build Automatically' is checked the R.java file should be created now.
To confirm, you can simply search the file in the project folder. You won't find it if its not being created.
If R.java is not being created, there is 90% chance that there is some issue with one of your interface *.xml files. Which usually resides in res->layout->activityXXXX.xml
Fix the interface file/s (try making it very simple with a single button or something) and R.java file will be generated by ADT automatically. At this point you should start noticing R.layout.XXXX not found type errors in your main program/activities. Which is a good thing, because now you can create/rename correct layout items and fix your project.
Just my 2 cents....
same happened to me, corrected checking any errors in res xml files, and changing target 1.5 to 2.2 android (on my case)
this usually happens to me when importing external code or online source code from examples or other developers.
Hi I had this happen to me this weekend.
The actual reason this had happened was that I had unchecked the update option for Android in the preferences as it wasn't working with the previous version of Eclipse. Now although the eclipse was seeming to run fine, when I PROJECT->CLEAN the project the R.java was missing.
Try re-ticking the android update option (WINDOW -> PREFERENCES -> INSTALL/UPDATE).
This completely solved my issue and I did not have to both with any hassle of scanning each line of my many xml files or my image resources.
Hope this helps someone :)
I just Encountered the same problem.
Turned out my Source control had inserted an Addedd>>>>>>>>>>>>>>>> section to my mainifest file.
All I had to do was fix the manifest file and then all the errors disappeared.
This happened to me while adding some new resources to my project.
The R file is build after every resource update.
The error in my case was incorrect file name. One of the image resources had a capital case letter in its name and that prevented the build from creating the R file. Changing the name simply worked.
I would suggest that when you add too many resources at a time, just make sure of the naming conventions for them. Its easy to miss such things.
Resource conventions: http://developer.android.com/guide/topics/resources/providing-resources.html
I had the similar problem. That was because I saved a drawable file picture.jpg as picture.png, as soon as I changed the name to previous one it started working. hope this would help someone in future

Categories

Resources