0

I added a translation to the strings.xml file so now I have 2 strings.xml files, one in hebrew and one in english (the original). After I did that, suddenly in the MainActivity.java file, every time there's R in the code, it shows in red color and says

Cannot resolve symbol 'R'

I tried all answers in other posts and it didn't help:

  • I tried cleaning the Gradle and sync it.
  • There are no xml errors
  • After adding import android.R; the error disappears but now the same error is shown for all xml file names (for example toastText = getString(R.string.negativeCups);, negativeCups is in red and shows this error.
  • I tried restarting Android Studio
  • I tried adding import com.howtoevery.justjava.R;
  • There are no files named with capital letters in the Res folder
  • I tried changing the compileSdkVersion, buildToolsVersion and targetSdkVersion
  • I tried adding import android.R.* and generally, I tried every answer in this post but nothing worked for me.

The code for the original strings.xml file:

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

The code for the translation:

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

Update: I even deleted the folder with the file of the localized strings.xml and still I have the same problem.

Community
  • 1
  • 1
Ishay Frenkel
  • 319
  • 1
  • 3
  • 17

2 Answers2

1

The values part

Create two different folder like the values in the picture, one called values for the default one(english) with all the values file and the strings.xml for the english strings and one called values-he with only the string.xml file for the hebrew.

Chris
  • 1,692
  • 2
  • 17
  • 21
  • It doesn't look like this inside of Android Studio, but in Finder it's the same as you described.. I got `values` folder and `values-he` folder and the files in the folders are just as expected (he in hebrew folder and regular in regular folder) – Ishay Frenkel Jun 06 '15 at 12:03
  • it's a picture i found on the internet and it's from eclipse but it's the same for android-studio – Chris Jun 06 '15 at 12:06
  • 2
    Please note that: `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".` Reference: http://developer.android.com/reference/java/util/Locale.html – Phantômaxx Jun 06 '15 at 12:06
  • @Chris, that's not what I'm talking about.. In android Studio it looks like it's in the same folder, but in Finder it's in different folders but it's still doesn't work. I also changed the folder name to iw instead of he and still nothing.. – Ishay Frenkel Jun 06 '15 at 12:10
  • @Chris I changed the Android view to Project view and now I see it this way: http://i.stack.imgur.com/kwwdp.png Anyways, I still have this problem.. – Ishay Frenkel Jun 06 '15 at 13:05
1

So after trying so many things that are related to this R problem, I looked at the logCat and saw that there is a problem inside the strings.xml file. Apparently I forgot to add the escape sign and this caused the problem, so this is what I had before:

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

and this is the fixed version:

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

That's it.. That's what's caused this problem and now it is fixed, thank you everyone!

Ishay Frenkel
  • 319
  • 1
  • 3
  • 17