Android Studio - error: cannot find symbol - setContentView(R.layout.activity_main)
Hi,
I'm learning to create something for Android in an Android studio and I'm trying to dissect one finished project to connect an Android application to a MySQL database using RestAPI. However, the application reports an error:
Please help
From what I've read, setContentView (R.layout.activity_main) means this:
R - resource
layout - The layout directory
activity_main - activity_main.xml template
But I don't understand why it doesn't work, because the activity_main.xml template is in the correct layout directory ...
However, I managed to solve it and here is the whole process of resolving the errors:
1.)
-------------------------------------------------- ----------------
error: cannot find symbol
getMenuInflater (). inflate (R.menu.menu_activity_main, menu);
^
symbol: variable menu
location: class R
2.)
-------------------------------------------------- ----------------
I added
# new error:
error: cannot find symbol
setContentView (R.layout.activity_main);
^
symbol: variable activity_main
location: class layout
3.)
-------------------------------------------------- ----------------
# I removed
# I gave a Clean project + Rebuild project
4.)
-------------------------------------------------- ----------------
# I added
# this returned an error
error: cannot find symbol
getMenuInflater (). inflate (R.menu.menu_activity_main, menu);
^
symbol: variable menu
location: class R
5.)
-------------------------------------------------- ----------------
# I made
res / menu
# and pasted there
menu_activity_main.xml
6.)
-------------------------------------------------- ----------------
# New error
error: cannot find symbol
if (id == R.id.action_settings) {
^
symbol: variable action_settings
location: class id
# I repaired
7.)
-------------------------------------------------- ----------------
# another error
error: cannot find symbol
btnSave = (Button) findViewById (R.id.btnSave);
^
symbol: variable btnSave
location: class id
# I added a button to res / layout / edit_product.xml
btn s id btnSave
8.)
-------------------------------------------------- ----------------
Miracle!!! The Android app has finally launched!
I'm learning to create something for Android in an Android studio and I'm trying to dissect one finished project to connect an Android application to a MySQL database using RestAPI. However, the application reports an error:
error: cannot find symbol
setContentView(R.layout.activity_main);
^
symbol: variable activity_main
location: class layout
Please help
REPLY
From what I've read, setContentView (R.layout.activity_main) means this:
R - resource
layout - The layout directory
activity_main - activity_main.xml template
But I don't understand why it doesn't work, because the activity_main.xml template is in the correct layout directory ...
However, I managed to solve it and here is the whole process of resolving the errors:
1.)
-------------------------------------------------- ----------------
error: cannot find symbol
getMenuInflater (). inflate (R.menu.menu_activity_main, menu);
^
symbol: variable menu
location: class R
2.)
-------------------------------------------------- ----------------
I added
import android.R;
# new error:
error: cannot find symbol
setContentView (R.layout.activity_main);
^
symbol: variable activity_main
location: class layout
3.)
-------------------------------------------------- ----------------
# I removed
import android.R;
# I gave a Clean project + Rebuild project
4.)
-------------------------------------------------- ----------------
# I added
import cz.muj_balicek.R;
# this returned an error
error: cannot find symbol
getMenuInflater (). inflate (R.menu.menu_activity_main, menu);
^
symbol: variable menu
location: class R
5.)
-------------------------------------------------- ----------------
# I made
res / menu
# and pasted there
menu_activity_main.xml
6.)
-------------------------------------------------- ----------------
# New error
error: cannot find symbol
if (id == R.id.action_settings) {
^
symbol: variable action_settings
location: class id
# I repaired
rest / menu / menu_activity_main.xml
xml version = "1.0" encoding = "utf-8"?>
7.)
-------------------------------------------------- ----------------
# another error
error: cannot find symbol
btnSave = (Button) findViewById (R.id.btnSave);
^
symbol: variable btnSave
location: class id
# I added a button to res / layout / edit_product.xml
btn s id btnSave
8.)
-------------------------------------------------- ----------------
Miracle!!! The Android app has finally launched!