Content won't resolve - android

I'm working through a book samples programs and i get the same problem whether I create and type the code or whether i download the code already typed, so i'm thinking it's a setup problem.
I get this error in each each class:
setContentView(R.layout.menu) where the "R" is underlined in red and the code won't compile. If i add import android.r like "QuickFix" suggests, the red line goes away from the "R", but then the red line appears under "menu" (in this example). The same behavior occurs in each of the 5 classes that make up the project.
I do have matching layouts for "menu" etc. And (to the best of my knowledge) I registered them as Application Nodes in the manifest file.
It's driving me nuts. Thanks for any suggestions.

You are not supposed to import android.R as it won't resolve your resources. Just remove that import and try to compile again.
If you are working off a tutorial then also check to have the same resources as in the tutorial and also if you decide to name thing different then keep that in mind an reference them by your names.

The R class is generated by Android and contains IDs for all resources in your res folder. Go to your AndroidManifest and locate the package= attribute on manifest. R is in this namespace. So if the namespace is com.yourpackage.blah, you'll want to import com.yourpackage.blah.R;

The problem was that for some reason Eclipse was not autogenerating the gen/R.java file. I don't know why. One time it did generate the file, and a after Project..Clean and Build All everything resolved. Thanks for the input.

Also make sure your file names under res, eg drawables, are OK. I had a file name with capital letter and that prevented the creation of R with the same reference errors that you got. I did not check the error messages and it took me a good 10-20 mns to find this out.

Sometimes this happens to me.
It usually happens that I have been working with some xml element, and before clicking to a java document to hit "run" i just hit "run" while on the xml. I don't really know what happens when you "run" an xml, but it makes this horible file called some_xml_name.out.xml.
DELETE THIS FILE!
This file is the reason why you get so many R related errors, since the Resources library is never really created, or something like that.
That is my advice, without actually seeing the error you're getting in the console.

Related

Android layout ids not recognised

I'm trying to get Android studio to recognise IDs of various views etc. in my layout.xml file. When I was inputting the code in the xml, everything seemed OK, but when I revisited my code today, the ids in the layout file were in red saying "cannot resolve symbol."
When I tried to access the view (eg using "view.findViewById(R.id.splo)"), it says it cannot resolve the symbol (in this case "splo").
I'm not doing anything radical, for instance this is how I define splo in my layout xml:
<TextView android:id="#+id/splo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" />
Doing some digging, I found that I don't have a res/values/id.xml file - and looking further I don't have anything below app/build/generated/source, so no R.java file either.
I've looked at some people's requests for help - one was very similar to mine ("cannot resolve symbol R" in Android Studio) but the solutions offered don't help.
I've done multiple invalidate cache and restart, with or without Build->clean project and nothing works, and the IDs are still not recognised. Can anyone help?
Well there might be potential errors in the package name when you are building your application.
Since you mentioned, you already tried rebuilding and cleaning the project, we won't waste time doing that again!
What I want you to do now is:
Open your AndroidMainfest.xml file
Scroll to the top and check is the package name is correct
If its correct then go to the Activity definitions in the manifest and check:
If the package name in these lines [android:name="YOUR_PACKAGE_NAME.YOUR_ACTIVITY_NAME"] is correctly assigned
If you see the above line is in this format [android:name=".YOUR_ACTIVITY_NAME]", then bring it in the above format by adding the package name before the dot.
Now clean and rebuild your project.
The problem should be solved and if not my request is that please
Upload your AndroidManifest.xml file and your package
information so that I can have a better look at the issue

My android app does not recognize R.string resources - what could be the reason?

I learn android with sample apps from textbook. In the given sample source R.string.something is recognized:
However, in my own EXACT source code, same R.strings are not recognized- highlighted as errors:
Does anyone know how to fix it?
I've had this many time with eclipse, and it was not a setup / code problem on my side. Generally, cleaning, rebuilding and sometime even stopping and restarting eclipse solved it. And yes, it's a pain in the neck ...
Try a clean of your project, if that does nothing then click on the problems tab and see if there's any build related issues, you may be missing a required jar or something!
These are following reason possible.
1-: You import android.R;
2-: Any error in xml files.
3-: Please check you String.xml file may be any error or declare a string more then two time.
I assume you are new to Android.
If so, there are two ways of using text strings in Buttons, textviews and so on:
1) Hardcoded string - you put the text you want in quotation marks (""), for example:
yourTextview.setText("Hardcoded string");
2) You can call the text from your String resources (res/values/strings.xml).
That is a much better approach seeing is is easier to translate, make changes and so on.
In your strings.xml file you can create all your string values, and call them from there.
Like in you example, if in your strings.xml file you have for example:
<string name="delete">This is String resource</string>
you can then call the string from there, like so :
yourTextview.setText(R.string.delete);
Hope this helps!

android developer, get error with webView1 = (WebView) findViewById(R.id.webView1); [duplicate]

I keep getting this error. Should I just make id a field?
My code is:
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
ImageView mainimage = (ImageView) findViewById(R.id.mainanim);
mainimage.setBackgroundResource(R.anim.mainanim);
mainanimation = (AnimationDrawable) mainimage.getBackground();
}
How exactly do you fix this?
Look at your import statements at the top. If you are saying import android.R, then there that is a problem. It might not be the only one as these 'R' errors can be tricky, but it would definitely definitely at least part of the problem.
If that doesn't fix it, make sure your eclipse plugin(ADT) and your android SDK are fully up to date, remove the project from the emulator/phone by manually deleting it from the OS, and clean the project (Launch Eclipse->Project->Clean...). Sounds silly to make sure your stuff is fully up to date, but the earlier versions of the ADT and SDK has a lot of annoying bugs related to the R files that have since been cleared up.
Just FYI, the stuff that shows up in the R class is generated from the stuff in your project res (aka resources) folder. The R class allows you to reference a resource (such as an image or a string) without having to do file operations all over the place. It does other stuff too, but that's for another answer. Android OS uses a similar scheme - it has a resources folder and the class android.R is the way to access stuff in the android resources folder. The problem arises when in a single class you are using both your own resources, and standard android resources. Normally you can say import at the top, and then reference a class just using the last bit of the name (for example, import java.util.List allows you to just write List in your class and the compiler knows you mean java.util.List). When you need to use two classes that are named the same thing, as is the case with the auto-generated R class, then you can import one of them and you have to fully qualify the other one whenever you want to mean it. Typically I import the R file for my project, and then just say android.R.whatever when I want an android resource.
Also, to reiterate Andy, don't modify the R file automatically. That's not how it's meant to be used.
Do not modify the R class. The error means there's something syntactically wrong with your XML layouts and R cannot be auto-generated. Try looking there and post the xml code you're not sure about, if any.
Edit : also: remove "import android.R" from imports at top of file (if there)
One possible solution:-
Summary: make sure you are using import com.yourpkgdomainname.yourpkgappname.R instead of import android.R
Details: The problem occured when I changed ID of a label which was being referred in other places in the layout XML file. Due to this error, the R file stopped generating at first. Eclipse is bad in handling errors with the layout files.
When I corrected the ID reference (with project clean few times and Eclipse restarts, I noticed that my import packages now has:
import android.R
Changing it to following fixed the error:
import com.example.app.R
I just fixed my problem right-clicking in the layout folder and clicking in the option Validate. Some windows will appear, you just clik OK and ist fine.
May be you created a new xml file in Layout Directory that file name containing a Capital Letter which is not allowed in xml file under Layout Directory.
Hope this help.
Just throwing this out there, but try retyping things manually. There's a chance that your quotation marks are the "wrong" ones as there's a similar unicode character which looks similar but is NOT a quotation mark.
If you copy/pasted the code snippits off a website, that might be your problem.
As Jake has mentioned, the problem might be because of copy/paste code.
Check the main.xml under res/layout. If there is no id field in that then you have a problem.
A typical example would be as below
<com.androidplot.xy.XYPlot
android:id="#+id/mySimpleXYPlot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10px"
android:layout_marginLeft="20px"
android:layout_marginRight="20px"
title="A Simple Example"
/>
Some times eclipse may confuse with other projects in the same directory.
Just change package name (don't forget to change in Android manifest file also), ensure the package name is not used already in the directory. It may work.
Just came across this myself.
Finally found my issue was with a .png file that I added that had a capital letter in it an caused exactly the same problem. Eclipse never flagged the file until I closed it and opened Eclipse back up.
What seems to be the problem, I just fixed mine in case anyone was wondering - Due to other errors i turned off build automatically, when i created a new project it said R.layout.main had an issue and needed to import R; So naturally as a novice, i did. Then i built manually and it had a problem with main. Try building your program as is, remove import R and it should be fine.
In main.xml (or wherever your item is defined) make sure that the ID for the R item is defined with #+id/... Here is an example with a button:
<Button android:text="B1" android:id="#+id/button_one"
android:layout_gravity="center_horizontal|center"
android:layout_height="fill_parent" android:layout_width="wrap_content" />
Each of these is important because:
# must precede the string
+ indicates it will create if not existing (whatever your item is)
I also had this error when I was working in a Java class once. My problem was simply that my xml file, with the references in it, was not saved. If you have both the xml file and java class open in tabs, check to make sure the xml file name in the tab doesn't have a * by it.
Hope this helps.
Just Clean your project so R will be generated automatically. This worked for me.
It gave me enough pain but I found the solution:
PROJECT ---> Clean ----> (Sele)
I had this problem but in my case it solved by restarting the eclipse.
For me the cause of the problem was that I had 2 images with the same name in my drawable-hdpi folder. Once I resolved that, then the R.java rebuilt itself and the problem was automatically solved.
select Project tab and click Build automatically so Build all option will be activated and then click on build all.and always start xml file name with lowercase.

How to add menu in Android without getting "R" error

I am writing a simple application to get familiar with android programming. Today I want to play with menus. I follow this page!. I first create "menu" folder under "res" and then add "game_menu.xml" in it. However this "R" thing can't get that folder. I tried some refresh to get it 'auto-generated' but it didn't work. I clean for a rebuild, but this time R.java completely dissappeared. How can I make eclipse to recreate R or what is the problem?
Are you sure the R is not creating the appropriate link for the menu?
To find your xml menu, you must type: "R.menu." & hit ctrl+space and all your menu's will pop up.
Try this way.
And if you want to check your R file, if its creating the menu link, search for:
"public static final class menu" in R file
In that class you will find your xml menu's..
are you able to compile ? Check which R you have imported in your code. It should not be the android.R but the one from your project
You have most likely misspelled something in your xml file, or have some other error. If the xml files are not entirely correct (or other resource files), the R. file cannot be built. Go through the file and make sure that everything is error free.
A common mistake is to name the resources with capitals, although the names can only contain small letters.
another common mistake is to use unsupported folder names.

"id cannot be resolved or is not a field" error?

I keep getting this error. Should I just make id a field?
My code is:
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
ImageView mainimage = (ImageView) findViewById(R.id.mainanim);
mainimage.setBackgroundResource(R.anim.mainanim);
mainanimation = (AnimationDrawable) mainimage.getBackground();
}
How exactly do you fix this?
Look at your import statements at the top. If you are saying import android.R, then there that is a problem. It might not be the only one as these 'R' errors can be tricky, but it would definitely definitely at least part of the problem.
If that doesn't fix it, make sure your eclipse plugin(ADT) and your android SDK are fully up to date, remove the project from the emulator/phone by manually deleting it from the OS, and clean the project (Launch Eclipse->Project->Clean...). Sounds silly to make sure your stuff is fully up to date, but the earlier versions of the ADT and SDK has a lot of annoying bugs related to the R files that have since been cleared up.
Just FYI, the stuff that shows up in the R class is generated from the stuff in your project res (aka resources) folder. The R class allows you to reference a resource (such as an image or a string) without having to do file operations all over the place. It does other stuff too, but that's for another answer. Android OS uses a similar scheme - it has a resources folder and the class android.R is the way to access stuff in the android resources folder. The problem arises when in a single class you are using both your own resources, and standard android resources. Normally you can say import at the top, and then reference a class just using the last bit of the name (for example, import java.util.List allows you to just write List in your class and the compiler knows you mean java.util.List). When you need to use two classes that are named the same thing, as is the case with the auto-generated R class, then you can import one of them and you have to fully qualify the other one whenever you want to mean it. Typically I import the R file for my project, and then just say android.R.whatever when I want an android resource.
Also, to reiterate Andy, don't modify the R file automatically. That's not how it's meant to be used.
Do not modify the R class. The error means there's something syntactically wrong with your XML layouts and R cannot be auto-generated. Try looking there and post the xml code you're not sure about, if any.
Edit : also: remove "import android.R" from imports at top of file (if there)
One possible solution:-
Summary: make sure you are using import com.yourpkgdomainname.yourpkgappname.R instead of import android.R
Details: The problem occured when I changed ID of a label which was being referred in other places in the layout XML file. Due to this error, the R file stopped generating at first. Eclipse is bad in handling errors with the layout files.
When I corrected the ID reference (with project clean few times and Eclipse restarts, I noticed that my import packages now has:
import android.R
Changing it to following fixed the error:
import com.example.app.R
I just fixed my problem right-clicking in the layout folder and clicking in the option Validate. Some windows will appear, you just clik OK and ist fine.
May be you created a new xml file in Layout Directory that file name containing a Capital Letter which is not allowed in xml file under Layout Directory.
Hope this help.
Just throwing this out there, but try retyping things manually. There's a chance that your quotation marks are the "wrong" ones as there's a similar unicode character which looks similar but is NOT a quotation mark.
If you copy/pasted the code snippits off a website, that might be your problem.
As Jake has mentioned, the problem might be because of copy/paste code.
Check the main.xml under res/layout. If there is no id field in that then you have a problem.
A typical example would be as below
<com.androidplot.xy.XYPlot
android:id="#+id/mySimpleXYPlot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10px"
android:layout_marginLeft="20px"
android:layout_marginRight="20px"
title="A Simple Example"
/>
Some times eclipse may confuse with other projects in the same directory.
Just change package name (don't forget to change in Android manifest file also), ensure the package name is not used already in the directory. It may work.
Just came across this myself.
Finally found my issue was with a .png file that I added that had a capital letter in it an caused exactly the same problem. Eclipse never flagged the file until I closed it and opened Eclipse back up.
What seems to be the problem, I just fixed mine in case anyone was wondering - Due to other errors i turned off build automatically, when i created a new project it said R.layout.main had an issue and needed to import R; So naturally as a novice, i did. Then i built manually and it had a problem with main. Try building your program as is, remove import R and it should be fine.
In main.xml (or wherever your item is defined) make sure that the ID for the R item is defined with #+id/... Here is an example with a button:
<Button android:text="B1" android:id="#+id/button_one"
android:layout_gravity="center_horizontal|center"
android:layout_height="fill_parent" android:layout_width="wrap_content" />
Each of these is important because:
# must precede the string
+ indicates it will create if not existing (whatever your item is)
I also had this error when I was working in a Java class once. My problem was simply that my xml file, with the references in it, was not saved. If you have both the xml file and java class open in tabs, check to make sure the xml file name in the tab doesn't have a * by it.
Hope this helps.
Just Clean your project so R will be generated automatically. This worked for me.
It gave me enough pain but I found the solution:
PROJECT ---> Clean ----> (Sele)
I had this problem but in my case it solved by restarting the eclipse.
For me the cause of the problem was that I had 2 images with the same name in my drawable-hdpi folder. Once I resolved that, then the R.java rebuilt itself and the problem was automatically solved.
select Project tab and click Build automatically so Build all option will be activated and then click on build all.and always start xml file name with lowercase.

Categories

Resources