简体   繁体   English

activity_main无法解析或无法解析

[英]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文件:

  1. one is your project's R.java file, which you would use to access resources/layout of your project. 一个是项目的R.java文件,您可以使用它来访问项目的资源/布局。
  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. 第二个是Android的R.java文件,其中包含动画,颜色和更多资源等本机资源的ID /索引,您可以使用android.R.color.black方式访问它们。

Mistake: 错误:

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

Solution: 解:

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类

  • problem is in the first line "import android.R" 问题出在第一行“import android.R”
  • use your applications 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM