Problems with R.layout.main - android

I am having a hard time trying to get my way around this. I just started a new project, and without doing anything the project shows this error at the mainactivity.java src, right at this line...
setContentView(R.layout.activity_main);
gives me "R cannot be resolve to a variable", ok, "quick fixes" gives me among others the option to import R, I go ahead and do it, then I got a new error " activity_main cannot be resolved or is not a field" and then quick fixes gives me just one option, "change to activity_list_item", so my question is: why all this is happening?
and then on this method
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
I get same error at R.menu.main, but now I do not get any options???
I am having a hard time with this R.java. I tried all possible fixes I found at this forum but none of them work.
help please. thanks

Sometimes, Eclipse will default to using android.R instead of com.your.package.R. Go to your imports section and check that you are importing your package's R file.
Then perform a directory refresh and a clean. This should fix your problem or expose another problem.
It could also be that you have cleaned your project and you have an error in some xml file, which will prevent R.java from being generated.

are you importing com.your.project.package.name and not com.actionbarsherlock for example ?

Remove import android.R; in your activity java file

Related

Android Studio: R file not getting updated

Upon creating a new menu resource file, the auto-generated R file is not getting updated. This causes syntax error when I try to use it the following way:
getMenuInflater().inflate(R.menu.menu, menu)
Attaching a screenshot for more clarity.
in the new android, can sometimes unrecognized faulty Id in xml or file xml.
I have met several such cases.
My solutions are close and open the project. you can try it.
Hope can help you
You can just invalidate and restart android studio even if it keep asking ignore and just run it.

how to add action buttons in android app

i m having problem in action buttons for my app. i did as per the tutorial from http://developer.android.com/training/basics/actionbar/adding-buttons.html but i get a red underlined error telling there is no file action_search in R.id and no methods openSearch() and openSettings() i fixed this by manually creating the methods and the action_search file and the app run on my device but the search button doesnot works and i now i cannot delete the action_search from R.id plz help
Have you tried running a clean on your build? Whenever you have issues with R. related things try to clean so the resources get rebuilt. This can sometimes be caused by copy and pasting chunks of code around...

R.layout.activity_find_contact cannot be resolved or is not a field

I am brand new to android development so please bear with me. I have encountered a problem when I create a new activity. In the newly created activity there is the auto-generated overridden method 'onCreate':
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_contact);
// Show the Up button in the action bar.
setupActionBar();
}
On the line setContentView(R.layout.activity_find_contact);, I get the error: activity_find_contact cannot be resolved or is not a field.
I have looked up other questions on this topic. Some say to just restart eclipse - this didn't work. Others said to try cleaning the project - this didn't work either. The most promising answer said to delete the line:
import android.R;
I tried this, and of course then R gets the error: R cannot be resolved to a variable. So I followed the advice to import your.application.package.R; - which in my context translates to import com.example.databasetest.R;. However this just brings up the error the import com.example.databasetest.R; cannot be resolved to a type.
So now I'm stumped and back to square one. Any ideas?
R files are automatically generated.. so you don't need to import it or do anything. The error is because there is something wrong in your xml file or files.Got through your xml files and see if you can find any errors.
These errors might even be typos.So make sure all of it is correct and cleaning and running the project again should get your code working
Do not use the following import
import android.R;
This errors comes because when we create new app we delete hello world textview but that is referenced in strings.xml in res > values > strings
go to that and remove hello world , clean project and errors should be gone ...

"R cannot be resolved" [duplicate]

This question already has answers here:
R cannot be resolved - Android error
(108 answers)
Closed 9 years ago.
I have tried finding the answer to this question on here before, but none of the fixes have worked for me. Basically, my Android project is refusing to run - even though I haven't edited it at all from the default program that is given when you make a new project.
I have a red line under R on Eclipse, throwing up the error "R cannot be resolved to a variable". It suggests that I import android.R, but when I do this, another red line comes up, this time under activity_main, saying it "cannot be resolved or is not a field".
I have absolutely no idea what is going on here, and am not prolific in coding or programming at all, so any answers in absolute layman's terms would be really great. My code so far is below:
package com.randomproject.thebasics;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Try doing a Project->Clean, and then a full re-build (Ctrl+B). Eclipse sometimes goes a bit wonky with auto-generated files.
As noted in the comments this question has been answered thousands of times. Well, ok, only tens of times. There are two possiblities:
1) Resource error
There is an error in your manifest or one of the files in your res directory. The resource compiler cannot run and there is no R.java class in the gen directory. Look in the gen folder, if there's no R.java file there, this is your problem.
2) Bad import
Sometimes in an attempt to help you, eclipse will add the import android.R to your imports and then collapse the import section so you can't see it. None of your references to your R.whatever resources will work after that. Check the imports. Delete android.R if it is there.

Error in layout name?

Iam using Jellybean. When i was putting layout name, this layout name is automatically created same as menu name. Then i access layout for main activity, i can't access id. I got error with main cannot resolved variable.
Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_first);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_my_first, menu);
return true;
}
I just recently ran into this problem as well.
It turned out that the generated name "activity_main" was not accepted. I removed the underscore and it worked. After I removed the _ underscore it was able to generate the R file.
Incidentally this was an error right from the originally generated code.
I had never seen this problem before.
Try cleaning your project!
Project - > Clean
Select "Build Automatically" so that every time you clean the project, it will be rebuilt. And yeah, make sure you are saving the file before cleaning it (I know it is weird but sometimes the errors refuse to go away until you save it and then Clean it). Make sure that there is no error in the files in the layout folder, as it sometimes prevents the auto-generation of the code in R.java
Also, make sure that your code in MainActivity.java does not have import android.R. Eclipse tends to add that when you are managing the imports.

Categories

Resources