Switch isChecked 布尔值 Android
Switch isChecked boolean Android
我有一个如下图所示的开关。
我输入联盟的名称,然后通过网络服务创建了一个联盟。
开关密码是。
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(...) 包含网络服务。我发布了六个变量。我感兴趣的是 open_league.
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);
}
我在这行之前打了个断点
sonObjectRequest jsonObjReq = new JsonObjectRequest(AppConfig.URL_CREATE_LEAGUE, jsonObject,
new com.android.volley.Response.Listener<JSONObject>()
检查我发送的json。所以当开关打开时我得到。
{
"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 json 变量已打开。但是,当开关关闭时,open_league 变量为空!为什么是这样?我已将 open_league 放入我的开关侦听器中,当它打开时关闭时关闭时 "OPEN" 。谢谢。
你的错误很简单,修正一下,
open_league = "CLOSED";
您尚未初始化变量但正在尝试使用它。
- 打开开关没有问题,因为变量已经赋值给"OPEN"
- 但是当您尝试在关闭状态下使用它时,您会得到
null
值,因为变量尚未初始化
永远记得在这种情况下将变量初始化为默认值。在您的情况下,默认值是 "CLOSED" 以将其初始化为 "CLOSED".
我有一个如下图所示的开关。
我输入联盟的名称,然后通过网络服务创建了一个联盟。
开关密码是。
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(...) 包含网络服务。我发布了六个变量。我感兴趣的是 open_league.
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);
}
我在这行之前打了个断点
sonObjectRequest jsonObjReq = new JsonObjectRequest(AppConfig.URL_CREATE_LEAGUE, jsonObject,
new com.android.volley.Response.Listener<JSONObject>()
检查我发送的json。所以当开关打开时我得到。
{
"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 json 变量已打开。但是,当开关关闭时,open_league 变量为空!为什么是这样?我已将 open_league 放入我的开关侦听器中,当它打开时关闭时关闭时 "OPEN" 。谢谢。
你的错误很简单,修正一下,
open_league = "CLOSED";
您尚未初始化变量但正在尝试使用它。
- 打开开关没有问题,因为变量已经赋值给"OPEN"
- 但是当您尝试在关闭状态下使用它时,您会得到
null
值,因为变量尚未初始化
永远记得在这种情况下将变量初始化为默认值。在您的情况下,默认值是 "CLOSED" 以将其初始化为 "CLOSED".