Android 开关被选中
我有一个开关,如下所示 我输入联盟的名称,然后通过web服务创建联盟 开关代码为Android 开关被选中,android,switch-statement,Android,Switch Statement,我有一个开关,如下所示 我输入联盟的名称,然后通过web服务创建联盟 开关代码为 aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
openLeague="OPEN";
}else{
openLeague = "CLOSED";
}
}
});
按钮代码
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
session = new SessionManager(getApplicationContext());
leagueName = teamTextField.getText().toString();
if (!leagueName.isEmpty()) {
createLeague(leagueName, username, password, start, end, openLeague);
} else {
Toast.makeText(getApplicationContext(),
"Please enter your details!", Toast.LENGTH_LONG)
.show();
}
}
});
createLeague(…)包含web服务。我发布了六个变量。我感兴趣的是公开赛
private void createLeague(final String leagueName, final String username, final String password,final String start,final String end,final String openLeague) {
String tag_json_obj = "json_obj_req";
final HashMap<String, String> postParams = new HashMap<String, String>();
postParams.put("league_name",leagueName);
postParams.put("username",username);
postParams.put("password",password);
postParams.put("league_start",start);
postParams.put("league_finish",end);
postParams.put("open_league",openLeague);
Response.Listener<JSONObject> listener;
Response.ErrorListener errorListener;
final JSONObject jsonObject = new JSONObject(postParams);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(AppConfig.URL_CREATE_LEAGUE, jsonObject,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
try {
if (response.getString("status").equals("success")){
Intent i = new Intent(CreateLeague.this, League.class);
startActivity(i);
finish();
}
} catch (JSONException e) {
Log.e("TAG", e.toString());
}
//pDialog.dismiss();
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("TAG", "Error: " + error.getMessage());
//pDialog.dismiss();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
// VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjRequest);
}
您可以看到open_变量是open的。但是,当开关闭合时,open_league变量为null!为什么会这样?我已将我的开关侦听器置于打开时为“打开”,关闭时为“关闭”。谢谢。你的错误很简单,只要解决这个问题
open_league = "CLOSED";
您尚未初始化变量,但正在尝试使用它
- 当开关打开时,没有问题,因为变量已指定为“打开”
- 但是,当您试图在关闭状态下使用它时,会得到
值,因为变量尚未初始化null
在这种情况下,请始终记住将变量初始化为默认值。在您的情况下,默认值是“CLOSED”,只是将其初始化为“CLOSED”。您是否已将open_league变量初始化为“CLOSED”初始值?哈哈,我没有!!!谢谢你!我以为我的web服务出了问题!!我刚刚正式回答了这个问题。当你的问题解决后,我希望你能接受并投赞成票。非常感谢。我确实把你的答案记为正确答案!
{
"league_start":"28-07-2015 09:37:43",
"username":"cerberus_30@gmail.com",
"league_finish":"11-08-2015 09:37:43",
"league_name":"Samsung League",
"password":"larissa",
"open_league":"OPEN"
}
open_league = "CLOSED";