Why do I get a StringFormat error with this?
-
21-12-2019 - |
Domanda
With this code (sbJava is a StringBuilder):
String androidFindLine = string.Format("Button _{0} = (Button) findViewById(R.id.{0});\"", btnId);
sbJava.Append(androidFindLine);
sbJava.Append(Environment.NewLine);
sbJava.Append(string.Format("_{0}.setOnClickListener(new View.OnClickListener() { ", btnId));
...I get,
System.FormatException was unhandled _HResult=-2146233033 _message=Input string was not in a correct format...
The value of btnId is "btnbutton_up"
What is the problem?
Soluzione
You should escape those {
with double {{
:
sbJava.Append(string.Format("_{0}.setOnClickListener(new View.OnClickListener() {{ ", btnId));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow