Android Studio 错误:无法解析 Xml 中的符号 [英] Android Studio error: cannot resolve symbol in Xml

查看:60
本文介绍了Android Studio 错误:无法解析 Xml 中的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 google Android Studio 的第一个 android 应用教程.但是当我尝试向我的应用程序添加搜索栏时,我现在遇到了 3 个奇怪的错误.

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.

我现在就在这里,我像教程一样添加了 XML 代码.

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

我得到的错误:

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').

在 android:showAsAction="ifRoom" 我收到奇怪的错误:

At android:showAsAction="ifRoom" I'm getting the weird error:

Excecution failed for task ':app:procesDebugResources'.

这是我的 XML 代码:

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>

这段代码有什么问题?

感谢阅读/帮助!

推荐答案

试试这个:

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

需要注意的几点:

1) 您应该将 android:showAsAction 标记更改为 app:showAsAction,这是为了与旧版本的 android 兼容.

1) You should change android:showAsAction tag to app:showAsAction, this is to do with compatibility with older versions of android.

2) 我已将您的搜索图标更改为嵌入在 Android 中的图标.您正在做的是尝试使用原生 Android 图标.自从您的教程以来,这似乎已经发生了变化.您始终可以使用自己的图标并将其放在项目的 res/drawable 文件夹中.

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) 正如@drawable 是对可绘制对象的引用一样,@string 是对 res/values 文件夹中的 strings.xml 文件的引用.您需要打开这个 xml 文件并添加如下内容:

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.

这篇关于Android Studio 错误:无法解析 Xml 中的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆