78

我在 android 应用程序中定义了一个字符串:

<string name="search_occurs">'{string}' occurs {times}</string>

这给出了错误:撇号前面没有\(在'{string}'发生{times})

我已经尝试了很多方法来解决这个问题:

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

我无法摆脱错误。我错过了什么?

4

8 回答 8

94

这些都应该根据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>
于 2013-03-29T14:52:33.053 回答
46

只需将 \ 放在字符串中的 ' 之前 这是 FALSE 它是 TRUE :)

于 2013-06-24T06:06:00.490 回答
3

为什么不提供'替换它的字符串?

你也知道这样的结构吗?

<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>
于 2013-03-29T14:52:21.157 回答
2

Replace Your apostrophe (') with (\') or add \ before every (')

note: copy only inside () text

于 2017-08-08T06:56:24.827 回答
2

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.

于 2017-08-28T09:06:18.113 回答
0

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

于 2017-03-26T14:22:54.343 回答
0

This Error Is Caused By An Apostrophe Used In String

Replace Apostrophe With \' Thats All

于 2017-07-20T07:16:11.730 回答
-1

I got the solution based on my scenario.

  1. select your string.xml file
  2. Right click and open show in explorer
  3. Edit the line from the string.xml file and save the code.
  4. 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.

于 2017-07-21T07:27:42.670 回答