我有一个如下所示的开关。
我输入联盟的名字,通过网络服务创建联盟。
开关代码是。
@font-face {
font-family: 'gothambook';
src: url(' {{ 'gothambook.eot' | asset_url }} ');
src: url(' {{ 'gothambook.eot?#iefix' | asset_url }} ') format('embedded-opentype'),
url(' {{ 'gothambook.woff2' | asset_url }} ') format('woff2'),
url(' {{ 'gothambook.woff' | asset_url }} ') format('woff'),
url(' {{ 'gothambook.ttf' | asset_url }} ') format('truetype'),
url(' {{ 'gothambook.svg#gothambook' | asset_url }} ') format('svg');
font-weight: normal;
font-style: normal;
}
按钮代码......
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
openLeague="OPEN";
}else{
openLeague = "CLOSED";
}
}
});
createLeague(...)包含Web服务。我发布了六个变量。我感兴趣的是open_league。
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();
}
}
});
我在此行之前设置了一个断点
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);
}
检查我发送的json。所以当开关打开时,我得到了。
sonObjectRequest jsonObjReq = new JsonObjectRequest(AppConfig.URL_CREATE_LEAGUE, jsonObject,
new com.android.volley.Response.Listener<JSONObject>()
您看到open_league json变量已打开。但是当开关关闭时,open_league变量为null!为什么是这样?我把我的开关监听器中的open_league设置为“OPEN”,当它处于OFF状态时CLOSED处于打开状态。感谢。
答案 0 :(得分:1)
你的错误很简单,只需解决这个问题,
open_league = "CLOSED";
您尚未初始化变量,但正在尝试使用它。
null
个值在这种情况下,始终记得将变量初始化为默认值。在您的情况下,默认值为“CLOSED”,只是将其初始化为“CLOSED”。