简体   繁体   中英

Android Studio MainActivity.java can't find resources

I'm basically writing my first hello world in android studio and the java file says that the xml layout file and other resources in the res file don't exist. I took the references to these things from books/tutorials so they should work.

The book said to use

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

But when that didn't work I changed it to res.layout.activity_main.xml

my project directory looks like this

How do I make it work?

http://i.stack.imgur.com/N71sl.png

//EDIT

http://i.stack.imgur.com/HUgsm.png

You are referencing res not R.

do something like this: setContentView( R.layout.activity_main);

Leave off the ".xml"

在稳定的频道中更新android studio并重新启动android studio并再次构建项目。

You need to understand the concept of the R class in android.
The R class is auto generated every time you build your project and cannot be modified.
This class contains a map of all the projects assets. Every time you created or import a resource, set a dimension, a string, create a layout, add or change id etc. the R file is changed in the background.

So if you want to access a resource you simply call it using the R class. For example:

layout: R.layout.activity_main
string: R.string.hello_world
id:     R.id.et_main_helloWorld

And so on. More info can be found here . Make sure you also check Providing Resources and Accessing Resources for a better understanding.

Good luck and happy coding.

最后,我开始了一个新项目,并逐个添加了所有内容,然后在values目录中放置了一个mp3文件,该文件弄乱了R文件。

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