简体   繁体   English

android“ main无法解析或不是字段”

[英]android “main cannot be resolved or is not a field”

im new to this platform, please help me to find what is the error... 我是这个平台的新手,请帮助我发现错误是什么...
setContentView(R.layout.main); 的setContentView(R.layout.main); // this line shows the error. //此行显示错误。

Code: 码:

package com.example.helloandroid;

import android.R;
import android.app.Activity;
import android.os.Bundle;

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

尝试删除import android.R行,然后清理您的项目。

Just adding more details on why the error is coming. 仅添加有关错误原因的更多详细信息。

As there is an import of android.R so setContentView() is looking for a layout file 'android.R.layout.main' and there is no main.xml in the layout files that come along with SDK. 由于存在android.R的导入,因此setContentView()正在查找布局文件“ android.R.layout.main”,并且SDK随附的布局文件中没有main.xml。 So, using the correct R.java import will work. 因此,使用正确的R.java导入将起作用。

clean Project then try To Run because i faced same problem before a month and remember layout/main.xml must be their and it must not contains any error. 清理项目,然后尝试运行,因为一个月前我遇到了同样的问题,请记住layout / main.xml必须是它们,并且其中不得包含任何错误。 you can Also Do this 你也可以这样做

import android.R; or 要么
import your.application.packagename.R; Now Clear Project and Run it. 现在清除项目并运行它。

You must have to simply change the 您必须只需更改

setContentView(R.layout.main);

... to: ... 至:

setContentView(R.layout.activity_main);

... because Layout contains this .xml file. ...因为Layout包含此.xml文件。

I hope your problem will be solved. 希望您的问题能得到解决。

First remove import android.R; 首先删除import android.R;

After any change on xml fiels you must clean project. 对xml字段进行任何更改后,您必须清理项目。

Build > Clean Project 构建>清洁项目

after that every things corrects. 之后,所有事情都会纠正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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