Reputation: 54033
From the [Android Getting Started][1] tutorial I'm trying to build my first app. This however, is quite a process. I was at the point "Add the Actions to the Action Bar" on [this page][2] when I encountered the error *main_activity_actions cannot be resolved or is not a field* for this line:
inflater.inflate(R.menu.main_activity_actions, menu);
So I found a similar post here on SO, which suggests to remove import android.R;
and then do Project -> Clean
and rebuild. Since I didn't have the import android.R;
in it I simply tried to add it, do a Clean and rebuild, but this leaves me with even more errors. So I removed the import android.R;
again and did a Clean and Rebuild again, but now it still gives me all those new errors, saying: R cannot be resolved to a variable
for the following lines (these lines do not appear close to eachother, but throughout the file):
setContentView(R.layout.activity_main);
inflater.inflate(R.menu.main_activity_actions, menu);
EditText editText = (EditText) findViewById(R.id.edit_message);
For reference, the total file is here on pastebin.
Can anybody please help me out here? What am I doing wrong and how can I solve this?
[EDIT] Below the code in my res/menu/main_activity_actions.xml. If you need to see any more code let me know!
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
<!-- Add some other thing, just cuz I can.. -->
<item android:id="@+id/action_something_else"
android:title="@string/action_something_else"
android:showAsAction="never" />
</menu>
Upvotes: 3
Views: 6898
Reputation: 1
I also had this problem, in my case the name of the xml file was main.xml not main_activity_actions
. Changing it from inflater.inflate(R.menu.main_activity_actions, menu);
to inflater.inflate(R.menu.main, menu);
fixed the problem.
Upvotes: 0
Reputation: 5835
If anyone is still having trouble on this, make sure that the XML file main_activity_actions.xml is in /res/menu/ and NOT in /res/layout
Upvotes: 1
Reputation: 1
Also, make sure you put the action_search and action_settings into values/strings.xml, in case they aren't there already.
<resources>
<string name="app_name">MyApp</string>
<string name="action_settings">Settings</string>
<string name="action_search">Search</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
I had the same problem, and I had stupidly not checked to see if action_search was defined. Also, the png referenced in the "android:icon" was missing from the "drawables" folder.
Upvotes: 0
Reputation: 1
Replace "R.menu" to be "R.layout" as below->>
inflater.inflate(R.layout.main_activity_actions, menu);
Upvotes: 0
Reputation: 35
In
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
give your main activity xml file name in setContentView(R.layout.your_activity_main);
Upvotes: 1
Reputation: 1537
Check your xml or check the res folder folder if there any problem with the image's name . Or check Window/showview/Errorlog
Upvotes: 1
Reputation: 1435
Check your all XML files inside res folder. You may having some error in one of your XML files
Upvotes: 0
Reputation: 196
Remove the import R file, then try Clean the project, then rebuild, should work.
Upvotes: 2