【问题标题】:Android Studio error: cannot resolve symbol in XmlAndroid Studio 错误:无法解析 Xml 中的符号
【发布时间】:2015-03-03 22:52:21
【问题描述】:

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

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

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" 我收到奇怪的错误:

Excecution failed for task ':app:procesDebugResources'.

这是我的 XML 代码:

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

这段代码有什么问题?

感谢阅读/帮助!

【问题讨论】:

  • 您的可绘制文件夹中有一个名为ic_action_search 的图标吗?
  • 文件夹中不仅有 ic_launcer.png:drawable-hdpi、drawable-mdpi、drawable-xhdpi 和 drawable-xxhdpi。正常的drawable文件夹是空的。
  • 那你为什么期望找到它?
  • @Stefan : 你在使用支持库 Action Bar 吗?
  • @squonk 我跟着添加了这个库,但我认为我做错了什么?如果我添加了这个库,我在哪里可以找到?

标签: java android xml android-studio


【解决方案1】:

试试这个:

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

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

3) 正如@drawable 是对您的drawable 的引用一样,@string 是对您的res/values 文件夹中的strings.xml 文件的引用。您需要打开这个 xml 文件并添加如下内容:

<string name="action_search">Search</string>

希望有帮助,祝你好运。

【讨论】:

  • 感谢您解决了这 3 个错误,但现在我在 java 代码中遇到了这个错误:无法解析符号“R”这是代码的样子:if (id == R.id. action_settings) { 为什么在我更改 XML 文件之前没有出现这个错误?
  • 嗯,无法解析符号 R 是一个棘手的符号,可能涉及范围很广。首先非常仔细地检查所有 res xml 是否有任何错误。然后尝试清理并重新构建。 stackoverflow.com/questions/17054000/…
  • 好吧,由于某种原因,刚刚重启我的 Android Studio 已经解决了这个问题谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多