Android Studio error: cannot resolve symbol in Xml

感情迁移 提交于 2021-02-07 10:01:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!