我在这个简单的code问题,我宣布一个String,想改变它里面的onCreate但此后的onCreate我有错误此令牌后VariableDeclaratorId预期!如果我把项目= 222里面的onCreate我得到111null333时,显示吐司这里是我的code
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" };}
推荐答案能否请你花一些时间,正确格式化你的问题?我没有看清楚,如果该项目=222;是一种方法里面还是不行。
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.
但是如果你的格式是正确,那么你的问题是,一个值项变量的分配不能在方法或code的静态{}块外完成。所以,如果你分配一个值之前,使用该项目的变量另一个对象或类变量这将是默认值(NULL对象和0或假的原始值)。
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).
希望这有助于。
更新:
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(); } }更多推荐
字符串声明VariableDeclaratorId预计此令牌后
发布评论