String declaration VariableDeclaratorId expected after this token

别来无恙 提交于 2019-12-11 21:19:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!