简体   繁体   中英

activity_main error

Activity_main.xml is present but still its giving a error in MainActivity.java Can anyone tell me what is the error here?

package com.example.hellotabwidget;

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

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

activity_main cannot be resolved or is not a field

remove the line

import android.R; 

and import the R for appropriate package

in this line

setContentView(R.layout.activity_main)

清理项目工作区并重建一次

All you need is simply change this line:

import android.R;

to this line (if com.example.hellotabwidget is base package of your app)

import com.example.hellotabwidget.R;

Golden sentence:

Take care of import android.R; , it imports built-in R file, it doesn't require in your project unless you want to access built-in resources.

More about: android.R or here .

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