Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First Android app: R cannot be resolved to a variable?

Tags:

android

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>
like image 463
kramer65 Avatar asked Dec 12 '25 05:12

kramer65


2 Answers

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

like image 198
user1509803 Avatar answered Dec 13 '25 19:12

user1509803


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

like image 22
playmaker420 Avatar answered Dec 13 '25 21:12

playmaker420



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!