简体   繁体   中英

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); // 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. So, using the correct R.java import will work.

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. 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.

I hope your problem will be solved.

First remove import android.R;

After any change on xml fiels you must clean project.

Build > Clean Project

after that every things corrects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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