Cannot get reference to layouts and id in res. folder - android

This question has been up a couple of times but still I haven't found an answer that helped me.
1:st one:
I cannot find any of my R.Layout.activity_article_detail and other layout xml files through reference. It stopped working after did a "clean project" in Eclipse and tried to build it up again.
I've tried removing and adding and removing the .R import but right now it wont find any of them. My layout files doesn't include a Capital letter, are spelled correcly.
code:
package martin.larsson.kopingsrssreader;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import martin.larsson.kopingsrssreader.R;
public class ArticleDetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article_detail);
Second problem is it cant find my res.menu either. I got 2 files in it. detailmenu.xml and refreshmenu.xml. In both i cant find any of the id tags
<item android:id="#+id/actionbar_markunread"
<item android:id="#+id/actionbar_saveoffline"
<item android:id="#+id/actionbar_refresh"
Does anyone got a clue?
SOLUTION: Uncheck "build automatically" and do a "clean project", and a Eclipse restart.

Clean your project or restart the eclipse.
Look to the imports, the R imported maybe is not correct.
If anything is wrong in your layouts, new updates aren't added to the R file. Look at the errors panel, and possibly to a clean rebuild to see what errors are popping up.

if u have an error in any of the resource files R will show up red unless all Errors are fixed and the project is built

Related

I don't know what happend

I was making an app in android studio and i had no errors. I closed android studio but reopened it because i forgot to do something. when i went into my MainActivity.java i noticed the R was in red and i don't know why
this is the code in my java, it's not like there was something in it:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class HomeMenu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_menu);
}
}
can someone help me ?
Ok, this is what I would do:
Clean the project
Rebuild the project
If that doesn't work, then:
Go to: File -> Invalidate Caches and Restart.
And if the previous option didn't work, then:
Check the package name in the first lines of the java files.
Create a new project and copy the content of your previous project.

In Android Studio 0.8.6, all the import statements say Cannot Resolve Symbol?

import android.app.Activity;//activity is in red
import android.os.Bundle;//bundle is in red
import android.view.Menu;//menu is in red
import android.view.MenuItem;//Menuitem is in red
import android.widget.Button;//Button is in red
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);//OnCreate is in red
//OnCreate is in red
It says it cannot resolve symbol for almost everything!
`
This seems to happen occasionally. Unfortunately, there are only a few things you can do.
First, try to clean the project...(Build -> Clean Project)
That, most likely, won't work. The next step would be to open your SDK manager (3rd button from the right) and download the latest build tools. This is very likely to work.
If that doesn't work, then you may just want to reinstall Android Studio. That's what I had to do two weeks ago.

R.layout.main not recognized

I'm trying to create the simplest hello world program but as soon as I create a new project, I get the following error: "main cannot be resolved or is not a field".
package com.example.helloworld;
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("Hello World!");
setContentView(tv);
}
}
I've tried adding import.android.R;, cleaning and restarting. I've also deleted import.android.R and included import.com.example.helloworld.R; (my package name). Keep getting the same error.
You've imported the Android's "R" class. You need to import your own.
Replace
import android.R;
With:
import com.example.helloworld.R;
If your R file doesn't resolve, then you have a problem in one of the files in your res folder. Check that all file names are lower case, have no illegal chars and that any XML files don't have syntax problems.
check your error logs or lint messages. I was facing this problem a couple of times.
Last time it was an improperly generated menu.xml file. Removing helped

The setContentView(R.layout.activity_main); is not working

My activity_main.xml gives me an error which says activity_main cannot be resolved or is not a field
setContentView(R.layout.activity_main);
I did Android 2.2 while unchecking the x on "Create Activity" so everything was empty and I had to fill it!
On your project explorer
click on resources,then
click on layout,then
you see a "file_name.xml" if not rename it to "file_name.xml" by pressing F2 ,
then replace
setContentView(R.layout.activity_main);
with
setContentView(R.layout."file_name");
dont forget to add
import "YOUR_PACKAGE_NAME".R;
above class decleration
It looks like u have imported android.R instead of that you import your package name else make it as
setContentView(packgname.R.layout.activity_main);
If you are importing
import android.R;
remove this line and then press ctrl+shift+o(for import necessary packages R file)
Your eclipse IDE
Porject---->clean>Build Automatically
Import packagename.R
by pressing ctr+shift+o

R cannot be resolved to a variable, rebuilding doesn't solve

Can someone help me?
public class Maths4to5Home extends Activity implements OnClickListener
{
private Button button_4to5PlayGame, button_mathsInstructions, button_4to5Scores;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maths4to5home); //error
button_4to5PlayGame = (Button)findViewById(R.id.button_4to5PlayGame); //error
button_mathsInstructions = (Button)findViewById(R.id.button_mathsInstructions); //error
button_4to5Scores = (Button)findViewById(R.id.button_4to5Scores); //error
I have included a sample of code above and highlighted where my errors are. I am being constantly told that R cannot be resolved to a variable. I have tried cleaning and rebuilding. Also I have tried importing R (as the quick fix suggests) but then I am told that for example in my setContentView() that
activity_maths4to5home cannot be resolved or is not a field
Any help would be greatly appreciated!
You need to import the correct R — specifically, the one for your project. This is based on what's in your project's manifest. If your manifest declares the project's package to be com.example.my_project then you need to import com.example.my_project.R;.
If your activity is already in the project's package, then you do not need to import R. However, you specifically need to not import android.R.
The error message may also be caused by a missing R.java. This can happen if errors in your resources prevents R.java from being generated. You should check that there are no errors in any of your resource files.

Categories

Resources