问题
i have problem in this simple code, I declare a String , and want change it inside onCreate but AFTER onCreate i have "VariableDeclaratorId expected after this token" Error !! And if i put item=222 inside onCreate i get "111null333" when Toast display here is my code
public class MainActivity extends Activity {
static String item;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), mStrings[0], Toast.LENGTH_SHORT).show();
}
item="222";
private String[] mStrings={
"111"+item+"333",
"test"
};
}
回答1:
Can you please spend some time and format your question correctly ? i do not see clearly if the item = "222"; is inside a method or not.
But if your formatting is "correct" then your problem is that the assignation of a value to the item variable can not be done outside of a method or the static{} block of code. And so if you use the item variable in another object or class variable before assigning it a value it will be the default value (null for objects and 0 or false for the primitive values).
Hope this helps.
UPDATE:
public class MainActivity extends Activity {
static String item;
private String[] mStrings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
item = "222";
mStrings = new String[2];
mStrings[0] = "111" + item + "333";
mStrings[1] = "test";
Toast.makeText(getApplicationContext(), mStrings[0], Toast.LENGTH_SHORT).show();
}
}
来源:https://stackoverflow.com/questions/19716378/string-declaration-variabledeclaratorid-expected-after-this-token