3

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>
2
  • If you got this then that means you are doing anything wrong in your .xml file. Check it whether it is ok or not. Commented Sep 10, 2013 at 9:35
  • 1
    do you have errors in your resource files? Commented Sep 10, 2013 at 9:35

8 Answers 8

2

Remove the import R file, then try Clean the project, then rebuild, should work.

Sign up to request clarification or add additional context in comments.

2 Comments

If by "rebuild, you mean hitting the "Run" button, I did this. Without success unfortunately.. Any more tips?
no not the run button, go to Project, uncheck Build Automatically, then click Build Project.
1

Check your xml or check the res folder folder if there any problem with the image's name . Or check Window/showview/Errorlog

1 Comment

Awesome, this was it! One png file was misnamed. Finally I saw it also in the errror log. Excuse me for not seeing that before!
1

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);

Comments

1

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

Comments

0

Check your all XML files inside res folder. You may having some error in one of your XML files

Comments

0

Replace "R.menu" to be "R.layout" as below->>

inflater.inflate(R.layout.main_activity_actions, menu);

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.