Android Eve
Android Eve

Reputation: 14974

main cannot be resolved or is not a field

This error occurs in setContentView line in this code snippet:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

I understand that in order for R.layout.main to be resolved, a file named res/layout/main.xml must be present.

It is present and valid (i.e. Eclipse marks it as perfect without any errors). So, what else could cause this error?

BTW, I already tried Project > Clean. The error persists.

Upvotes: 32

Views: 89567

Answers (9)

Engr Muhammad Zohaib
Engr Muhammad Zohaib

Reputation: 121

for me changing

import org.opencv.R;

to

import com.example.opencvtryagain.R;

solved it, where as my package name was com.example.opencvtryagain;

Upvotes: 1

Haris
Haris

Reputation: 14053

I changed

   import android.R;

to

 import your.application.packagename.R;

And project ->clean still the error.

But when I Run the project the error automatically gone, may be Eclipse problem.

Upvotes: 1

dev_android
dev_android

Reputation: 8818

I have removed the following line and it worked for me.

import android.R;

Upvotes: 0

yogesh
yogesh

Reputation: 21

Delete all your import statements from the class where you are getting this error message.

Then press ctrl+shift+o

Upvotes: 2

Joseph Selvaraj
Joseph Selvaraj

Reputation: 2237

If you are using eclipse, then

  1. Delete all your import statements from the class where you are getting this error message.

  2. press CTRL + SHIFT + O --> OR select Source from menu and select Organize import. This will import all the necessary classes.

Note: Since you have a local R.class file, it will import your local file instead of the android.R file.

Hope this will help someone.

Upvotes: 0

PIR FAHIM SHAH
PIR FAHIM SHAH

Reputation: 21

If this is the problem you are encountering then check at the top of your code - you will see:

import android.R

Delete that line and and change it into

import (com.xx.yy)

Replace (com.xx.yy) with your actual package name for the class. This problem occurs mainly when you copy all the XML and Java code of another application and paste it into your new application.

This is an example of how I replaced the android.R packages within one of my own apps (a puzzle game):

package com.pir.PUZZLEGAME_NEW;

import com.pir.PUZZLEGAME_NEW.*;
import com.pir.puzzlegame_old.R;

Good luck!

Upvotes: 2

giorgioca
giorgioca

Reputation: 713

Also try: add

import your.application.packagename.R;

and run: eclipse->project->clean..

The error should be gone.

Upvotes: 10

Also, sometimes it still won't compile even after making that needed change; but if you go ahead and run the app, the errs will clear up.

Upvotes: 0

Cristian
Cristian

Reputation: 200080

Make sure you don't have this in your imports:

import android.R;

but:

import your.application.packagename.R;

Upvotes: 140

Related Questions