问题
I'm following the google Android Studio first android app tutorial. But I get 3 weird errors now when trying to add a search bar to my app.
I'm here right now and I added the XML code just like the tutorial.
http://developer.android.com/training/basics/actionbar/adding-buttons.html
The errors I get:
Error:(5, 23) No resource found that matches the given name (at 'icon' with value '@drawable/ic_action_search').
Error:(6, 24) No resource found that matches the given name (at 'title' with value '@string/action_search').
At android:showAsAction="ifRoom" I'm getting the weird error:
Excecution failed for task ':app:procesDebugResources'.
This is my XML code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
Whats wrong in this code?
Thanks for reading/helping!
回答1:
Try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/action_search"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
A few things to note:
1) You should change android:showAsAction tag to app:showAsAction, this is to do with compatibility with older versions of android.
2) I've changed your search icon to one embedded in Android. What you're doing is trying to use a native Android icon. Seems like this has changed since your tutorial. You can always use your own icon and put it in the res/drawable folders in your project.
3) Just as @drawable is a reference to your drawables, @string is a reference to the strings.xml file in your res/values folder. You need to open this xml file and add something like:
<string name="action_search">Search</string>
Hope that helps, good luck.
来源:https://stackoverflow.com/questions/27795830/android-studio-error-cannot-resolve-symbol-in-xml