Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

activity_main cannot be resolved or not a field

Tags:

java

android

I am a beginner in android and I was trying to make a simple application on android and this "activity_main" error is not getting cleared.I have gone through all the available answers

import android.R;
import android.app.Activity;
import android.os.Bundle;
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.main, menu); return true;
  }
}
like image 532
zamil nizar Avatar asked May 16 '13 10:05

zamil nizar


2 Answers

Clean project then try to run it. I faced the same problem. You can also do this:

Remove from your code following imports import android.R; or import your.application.packagename.R;

Now clean the project and run it.

like image 127
Sagar Maiyad Avatar answered Sep 21 '22 15:09

Sagar Maiyad


Let me explain(put more detail) what exactly is happening with the above code.

FYI, there are two types of R.java file exists:

  1. one is your project's R.java file, which you would use to access resources/layout of your project.
  2. second is Android's R.java file which contains ID/index of the native resources like anim, color and many more resources, which you can access by using android.R.color.black way.

Mistake:

Now, you are making mistake by importing android.R which is not allowing you to access main_activity.xml of your project.

Solution:

Remove android.R and import R file of your project. You can press CTRL + SHIFT + O simply, will display available options.

like image 25
Paresh Mayani Avatar answered Sep 18 '22 15:09

Paresh Mayani