【问题标题】:Android Studio cannot resolve symbol 'R' after translating strings.xml [duplicate]翻译strings.xml后,Android Studio无法解析符号'R' [重复]
【发布时间】:2015-08-21 07:28:13
【问题描述】:

我在strings.xml 文件中添加了翻译,所以现在我有2 个strings.xml 文件,一个是希伯来文,一个是英文(原版)。 在我这样做之后,突然在MainActivity.java文件中,每次代码中有R时,它都会显示为红色并显示

Cannot resolve symbol 'R'

我尝试了其他帖子中的所有答案,但没有帮助:

  • 我尝试清理 Gradle 并同步它。
  • 没有 xml 错误
  • 添加import android.R; 后错误消失,但现在所有xml 文件名都显示相同的错误(例如toastText = getString(R.string.negativeCups);negativeCups 为红色并显示此错误。
  • 我尝试重启 Android Studio
  • 我尝试添加import com.howtoevery.justjava.R;
  • Res 文件夹中没有以大写字母命名的文件
  • 我尝试更改compileSdkVersionbuildToolsVersiontargetSdkVersion
  • 我尝试添加import android.R.* 通常,我在this 帖子中尝试了所有答案,但对我没有任何帮助。

原始strings.xml文件的代码:

<resources>
    <string name="app_name" translatable="false">Just Java</string>

    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="quantity">Quantity</string>
    <string name="order">Order</string>
    <string name="add">Add another cup of coffee to your purchase</string>
    <string name="reset">Reset calculation</string>
    <string name="decrease">remove 1 cup</string>
    <string name="ordering">You are going to order</string>
    <string name="ordering_cups_price">cups of coffee at a price of</string>
    <string name="empty_order">You can't order nothing, please buy at least 1 cup of coffee</string>
    <string name="resetted">Cups of coffee is already reset.</string>
    <string name="negativeCups">You can't have negative number of cups of coffee</string>
</resources>

翻译代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="add">תוסיף עוד כוס קפה להזמנה</string>
    <string name="decrease">תסיר כוס אחת</string>
    <string name="hello_world">שלום עולם!</string>
    <string name="order">הזמן!</string>
    <string name="quantity">כמות</string>
    <string name="reset">אפס כמות</string>
    <string name="action_settings">הגדרות</string>
    <string name="ordering">אתה עומד להזמין</string>
    <string name="ordering_cups_price"> כוסות קפה במחיר של </string>
    <string name="empty_order">מה אתה פסיכי? אי אפשר להזמין כלום</string>
    <string name="resetted">משעמם לך? לחצת כבר איפוס יא חופר</string>
    <string name="negativeCups">טוב מה נהיה?? אתה רוצה כבר למכור כוסות?</string>
</resources>

更新: 我什至删除了带有本地化strings.xml 文件的文件夹,但我仍然遇到同样的问题。

【问题讨论】:

  • 您检查资源是否有任何错误?或者尝试重建你的项目。
  • 你需要检查你的资源文件。可能存在一些格式或资源错误。
  • 显示您的两个 strings.xml 文件。
  • 那些 strings.xml 文件被放置在两个单独的目录中,values 是英文的,values-he 是希伯来语的?

标签: java android xml android-studio


【解决方案1】:

创建两个不同的文件夹,如图片中的值,一个称为默认值(英语),包含所有值文件,strings.xml 用于英文字符串,另一个称为 values-he,只有 string.xml希伯来文文件。

【讨论】:

  • 它在Android Studio中看起来不像这样,但在Finder中它和你描述的一样。我得到values文件夹和values-he文件夹,文件夹中的文件只是正如预期的那样(他在希伯来语文件夹中,常规文件夹中)
  • 这是我在互联网上找到的一张图片,它来自 eclipse,但对于 android-studio 是一样的
  • 请注意:Java uses several deprecated two-letter codes. The Hebrew ("he") language code is rewritten as "iw", Indonesian ("id") as "in", and Yiddish ("yi") as "ji".参考:developer.android.com/reference/java/util/Locale.html
  • @Chris,这不是我要说的。在 android Studio 中,它看起来像在同一个文件夹中,但在 Finder 中,它在不同的文件夹中,但它仍然不起作用。我还将文件夹名称更改为 iw 而不是他,但仍然没有..
  • @Chris 我将 Android 视图更改为项目视图,现在我这样看:i.stack.imgur.com/kwwdp.png 无论如何,我仍然有这个问题..
【解决方案2】:

所以在尝试了很多与这个R问题相关的事情之后,我查看了logCat,发现strings.xml文件内部存在问题。显然我忘了添加转义符号,这导致了问题,所以这是我以前的:

<string name="negativeCups">You can't have negative number of cups of coffee<string>

这是固定版本:

<string name="negativeCups">You can\'t have negative number of cups of coffee<string>

就是这样..这是造成这个问题的原因,现在已经解决了,谢谢大家!

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 2016-07-31
    • 2019-03-26
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    相关资源
    最近更新 更多