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

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

Related

setContentView(R.layout.main); - cannot find package

I have never used Android Studio before and have always been coding using NetBeans.
I need to create an app for Android, so I decided to use NetBeans. I created new project and it is 'empty' for now, however I have an error:
package Fridge.Freezer;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
The error is in setContentView(R.layout.main); - cannot find package. Can anyone tell what I am doing wrong? Clean and build does not work.
erase your package name at the top and try to add it to getting the suggestion package import by shortcut Control + Enter beside "R"
Try checking following component in your project directory
R.java file
folder named layout
layout xml file named main.xmlmain
PS Since your are starting a fresh project, I would suggest you to start at Android Studio.
I tried cleaning the project and rebuilding it again. It did not work. The solution was to restart IDE.

R.layout Android 2.2.3

Why am i having to go all the way in my class so he can recognize this layout i created? Because it simply does not recognize as R.layout.my_layout
OK:
super(context, com.example.leonardoinhoqui.tcc2.R.layout.my_layout);
Problem:
super(context,R.layout.my_layout);
Most probably you have a wrong import in your code (this sometimes happen when Android Studio resolves imports)
import android.R.*;
and because of that you have to provide the full name of your own R package

Cannot get reference to layouts and id in res. folder

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

'Intent cannot be resolved to a type' error in eclipse

I am programming the first android tutorial in eclipse, and when compiling this code:
Intent intent = getIntent();
it gives the error
Intent cannot be resolved to a type
How do I fix it?
Probably, the import statement is missing. Try pressing Ctrl+Shift+O, Eclipse will automatically add the import statement if missing.
Then look at the top of the file and see what has been added. It should be something like this:
import android.content.Intent;

how to import com.android.mms.R?

In some class of android source code ,they import com.android.mms.R, but I can't do it after I tryied. Are there any methods be used to import com.android.mms.R?
If your class already imported import com.android.R, delete it. and clean the project rebuild it. from projects>clean
if you have used any of Res by R.XXXX your project R will automatically get imported or do ctrl + shift + O
Also if it doesn't work go to property(right click on project)>Android here select target version as 4.0, then clean it will go.. first remove import com.android.R line from class

Categories

Resources