Jem
Jem

Reputation: 75

R.main cannot be resolved - None of the other answers have worked

So like others I have this little bit of code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

The problem is that main cannot be resolved or is not a field. main in the code is underlined in red.

What I have tried due to other answers:

  1. Inserting an import com.project.appname.R;
  2. Insured there is no import android.R;
  3. Deleted all the imports and then pressed shift+ctrl+o (then checking import android.R was not inserted)
  4. Checking my layout in the folder res-->layout --> activity_main.xml file
  5. I also tried to change the code to include activity_main instead of just main.

I have cleaned the project and rebuilt it every time I tired each suggestion and error has still not gone away.

I am using Eclipse, Juno, as well as Android and Google plugins.

Any suggestions?

Edit: Ok. In the folder res>layout> activity_main.xml is the correct name.

I have tried to type

setContentView(R.layout.activitymain); and setContentView(R.layout.activity_main);

but the error is still there.

When I type R.layout. and then wait for the suggestions the first thing on the list is activity_list_item : int - R.layout (There is no other suggestions in the list that include the word main or activity?)

Cannot attach list due to only being new on here and not enough reputation

Upvotes: 0

Views: 945

Answers (2)

codeMagic
codeMagic

Reputation: 44571

This

setContentView(R.layout.main);

needs to be

setContentView(R.layout.activitymain);

Since that is the name of your xml file that is what you inflate in setContentView(). It is currently looking in "res/layout" for a file named main.xml when it should be looking for activitymain.xml or whatever the name of the xml file is that you want to use for your layout

Upvotes: 1

labatyo
labatyo

Reputation: 486

change

setContentView(R.layout.main);

to

setContentView(R.layout.activitymain);

Unless you have a layout called main.xml in your layout folder

Upvotes: 0

Related Questions