[英]activity_main cannot be resolved or not a field
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 我是android的初学者,我试图在android上创建一个简单的应用程序,这个“activity_main”错误没有被清除。我已经完成了所有可用的答案
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;
}
}
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;
import android.R;
从代码中删除import android.R;
or import your.application.packagename.R;
或者
import your.application.packagename.R;
Now clean the project and run it. 现在清理项目并运行它。
Let me explain(put more detail) what exactly is happening with the above code. 让我解释(详细说明)上面的代码究竟发生了什么。
FYI, there are two types of R.java
file exists: 仅供参考,存在两种类型的
R.java
文件:
R.java
file, which you would use to access resources/layout of your project. R.java
文件,您可以使用它来访问项目的资源/布局。 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. R.java
文件,其中包含动画,颜色和更多资源等本机资源的ID /索引,您可以使用android.R.color.black
方式访问它们。 Now, you are making mistake by importing android.R
which is not allowing you to access main_activity.xml
of your project. 现在,您通过导入
android.R
错误,这不允许您访问项目的main_activity.xml
。
Remove android.R
and import R file of your project. 删除
android.R
并导入项目的R文件。 You can press CTRL + SHIFT + O simply, will display available options. 您可以简单地按CTRL + SHIFT + O ,将显示可用选项。
U seem to have imported wrong R.java class 你好像导入了错误的R.java类
Make sure you have the activity_main.xml file spelled correctly. 确保您正确拼写了activity_main.xml文件。 I experienced the same problem due to having a misspelled file name.
由于文件名拼写错误,我遇到了同样的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.