0
votes

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

4

4 Answers

2
votes

You are referencing res not R.

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

Leave off the ".xml"

0
votes

update your android studio in stable channel and restart android studio and build the project again .

0
votes

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.

0
votes

In the end I started a new project and added in everything piece by piece, I had put a mp3 file in the values directory which was messing up the R file.