Error:(218) Apostrophe not preceded by \ - android

In my strings.xml, I have made a string containing (it's) and I get this error.
Error:(218) Apostrophe not preceded by \
Yes, I know I have to either place a \ before the apostrophe, or just delete the apostrophe, but my problem is that when I run the app using (it\s) or just deleting it, it comes back; even if I delete the whole string, it all comes back when I click run. How do I stop this?
Here's the String but it really doesn't matter. I just want it to be deleted and not restored when I run the app.
<string> name="talk" So, you want these things? LOL its gonna</string>

When the error occurs, Android Studio opens the values.xml which, at that point in the file, deceptively looks exactly the same as your strings.xml. Make sure you're updating the strings.xml and you should be good to go.

Actually, there are two files strings.xml and values.xml
We edit our string data in strings.xml and whenever we getting the error like Apostrophe not preceded by \, When you click on error android studio redirect you to values.xml and you are editing in values.xml and android studio just deleted what you edited.
So make sure you are editing strings.xml not values.xml

Place your string in double quotes.<string>"Your text 'here'"</string>

try
<string name="talk">your string value</string>

Related

Android studio won't change string

In my strings.xml file I had this string:
<string name="lets_do_this">Let's Do This</string>
This gave me an error of:
Error:(897) Apostrophe not preceded by \ (in Let's Do This)
So I changed the string to this:
<string name="lets_do_this">Let\'s Do This</string>
But every time I build the project, the string keeps changing back to the first version for some reason and I keep getting that error without the ability to change the string. Why is this happening?
If it changes back, sounds like you changed the strings xml file that is located within the build folder, but you need to edit your own within res/values of the app module.
If you have an apostrophe (') in your string, you must either escape it with a backslash (\') or enclose the string in double-quotes ("").
see
Formatting and Styling - String Resources
<string name="lets_do_this">"Let's Do This"</string>

Resource name must Start with a letter

I am having this issue when I am having file with name 01a.png under res/drawable.
The error looks like:
Error: The resource name must start with a letter
I am using build:gradle:2.1.2 and SDK version 24.
Any solution to fix this without changing filename and without degrading SDK version?
change the name of the resources, it MUST start with a letter
Imagname must starts with a letter so change the image name
Resources are located on the left in your Android Studio.
Project > app > res > values.. you'll find your resources there. Android studio will commonly mark the incorrect syntax with RED text
In My Case when the string key name got removed from string.xml so I got this error,
for example:
<string name="">Submit Order</string>
after putting back string key name like below, it's working fine
<string name="label_submit_order">Submit Order</string>

Apostrophe not preceded by \

I have a string defined in an android application:
<string name="search_occurs">'{string}' occurs {times}</string>
This gives the error: Apostrophe not preceded by \ (in '{string}' occurs {times})
I've tried a number of things to fix this:
<string name="search_occurs">\'{string}\' occurs {times}</string>
<string name="search_occurs">&apos;{string}&apos; occurs {times}</string>
<string name="search_occurs"><![CDATA['{string}' occurs {times}]]></string>
<string name="search_occurs"><![CDATA[\'{string}\' occurs {times}]]></string>
I'm not able to get rid of the error. What am I missing?
these should both work according to http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling:
<string name="search_occurs">\'{string}\' occurs {times}</string>
<string name="search_occurs">"'{string}' occurs {times}"</string>
just put \ before ' in your string
It's FALSE
It\'s TRUE :)
Why don't you provide the ' with the string you replace it with?
Also you know constructs like this?
<string name="profile_age_range_text_between">between %1$d and %2$d</string>
<string name="unit_height_inches">%1$d\'%2$d ft</string>
<string name="unit_distance_miles">%s mi</string>
Replace Your apostrophe (') with (\')
or
add \ before every (')
note: copy only inside () text
Make sure that you are editing the correct file. As this answer states, when you get the error, Android Studio opens a values.xml file, which looks similar to your original file. You have to add the \ in the original strings.xml file (or wherever the offending character is). Otherwise it will just keep repeating the error.
In answer to https://stackoverflow.com/users/3339394/the-martian
There is general solution for replacing ' by \' you have to copy the text into a word editor(like Notepad ++) and then by using the "replace" option in the program replace whole 's with \' .
(cause I've not enough reputation i had to write it in an answer sorry!)
This Error Is Caused By An Apostrophe Used In String
Replace Apostrophe With \' Thats All
I got the solution based on my scenario.
select your string.xml file
Right click and open show in explorer
Edit the line from the string.xml file and save the code.
Reopen Android studio and refresh the solution.
Now the code will run.
Note: I got the error in using Apostrophe on can't. so i change into Cannot. Now my scenario will works.

How to overcome the fault in apostrophe strings.xml in android? [duplicate]

I have declared a long string in string.xml of an application.
Declared like this
<string name="terms">PLEASE READ THESE TERMS OF USE CAREFULLY BY ACCESSING THIS .................</string>
But this gives the following error :
error: Apostrophe not preceded by \ (in PLEASE READ THESE TERMS OF USE CAREFULLY
post your complete string. Though, my guess is there is an apostrophe (') character in your string. replace it with (\') and it will fix the issue. for example,
//strings.xml
<string name="terms">
Hey Mr. Android, are you stuck? Here, I\'ll clear a path for you.
</string>
Ref:
http://www.mrexcel.com/forum/showthread.php?t=195353
https://code.google.com/archive/p/replicaisland/issues/48
Solution
Apostrophes in the strings.xml should be written as
\'
Example
In my case I had an error with this string in my strings.xml and I fixed it.
<item>Most arguments can be ended with three words, "I don\'t care".</item>
Here you see my app builds properly with that code.
Here is the actual string in my app.
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Escaping apostrophes and quotes
If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don't work:
<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>
You have to put \ before an apostrophe. Like this \' , Also check that you are editing strings.xml and not values.xml (android studio directs you to this file when shows the error). Because if you edit values.xml and try to compile again, the error persists. This was happening to me recently.
Use this regex (?<!\\)' for searching an unescaped apostrophe.
It finds an apostrophe that not preceded by a backslash.
I use hebrew(RTL language) in strings.xml.
I have manually searched the string.xml for this char: '
than I added the escape char \ infront of it (now it looks like \' ) and still got the same error!
I searched again for the char ' and I replaced the char ' with \'(eng writing) , since it shows a right to left it looks like that '\ in the strings.xml !!
Problem solved.
The best answer of simply escaping the ' with \' works but I do not like the solution because when using replace all with ' to \', it only works once. The second time you'll be left with \\'.
Therefore, use: \u0027.
You may be able to use unicode equivalent both apostrophe and other characters which are not supported in xml string. Apostrophe's equivalent is "\u0027" .
1. for error: unescaped apostrophe in string
what I found is that AAPT2 tool points to wrong row in xml, sometimes.
So you should correct whole strings.xml file
In Android Studio, in problem file use :
Edit->find->replace
Then write in first field \' (or \&,>,\<,\")
in second put '(or &,>,<,")
then replace each if needed.(or "reptlace all" in case with ')
Then in first field again put '(or &,>,<,")
and in second write \'
then replace each if needed.(or "replace all" in case with ')
2. for other problematic symbols
I use to comment each next half part +rebuild
until i won't find the wrong sign.
E.g. an escaped word "\update" unexpectedly drops such error :)
To Find all the ' in your file, do the following:
Ctrl + F [Enter in the search field apostrophe] '
' should be written as "World's"

Error in strings.xml file in Android

I have declared a long string in string.xml of an application.
Declared like this
<string name="terms">PLEASE READ THESE TERMS OF USE CAREFULLY BY ACCESSING THIS .................</string>
But this gives the following error :
error: Apostrophe not preceded by \ (in PLEASE READ THESE TERMS OF USE CAREFULLY
post your complete string. Though, my guess is there is an apostrophe (') character in your string. replace it with (\') and it will fix the issue. for example,
//strings.xml
<string name="terms">
Hey Mr. Android, are you stuck? Here, I\'ll clear a path for you.
</string>
Ref:
http://www.mrexcel.com/forum/showthread.php?t=195353
https://code.google.com/archive/p/replicaisland/issues/48
Solution
Apostrophes in the strings.xml should be written as
\'
Example
In my case I had an error with this string in my strings.xml and I fixed it.
<item>Most arguments can be ended with three words, "I don\'t care".</item>
Here you see my app builds properly with that code.
Here is the actual string in my app.
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Escaping apostrophes and quotes
If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don't work:
<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>
You have to put \ before an apostrophe. Like this \' , Also check that you are editing strings.xml and not values.xml (android studio directs you to this file when shows the error). Because if you edit values.xml and try to compile again, the error persists. This was happening to me recently.
Use this regex (?<!\\)' for searching an unescaped apostrophe.
It finds an apostrophe that not preceded by a backslash.
I use hebrew(RTL language) in strings.xml.
I have manually searched the string.xml for this char: '
than I added the escape char \ infront of it (now it looks like \' ) and still got the same error!
I searched again for the char ' and I replaced the char ' with \'(eng writing) , since it shows a right to left it looks like that '\ in the strings.xml !!
Problem solved.
The best answer of simply escaping the ' with \' works but I do not like the solution because when using replace all with ' to \', it only works once. The second time you'll be left with \\'.
Therefore, use: \u0027.
You may be able to use unicode equivalent both apostrophe and other characters which are not supported in xml string. Apostrophe's equivalent is "\u0027" .
1. for error: unescaped apostrophe in string
what I found is that AAPT2 tool points to wrong row in xml, sometimes.
So you should correct whole strings.xml file
In Android Studio, in problem file use :
Edit->find->replace
Then write in first field \' (or \&,>,\<,\")
in second put '(or &,>,<,")
then replace each if needed.(or "reptlace all" in case with ')
Then in first field again put '(or &,>,<,")
and in second write \'
then replace each if needed.(or "replace all" in case with ')
2. for other problematic symbols
I use to comment each next half part +rebuild
until i won't find the wrong sign.
E.g. an escaped word "\update" unexpectedly drops such error :)
To Find all the ' in your file, do the following:
Ctrl + F [Enter in the search field apostrophe] '
' should be written as "World's"

Categories

Resources