我有以下类,我想稍微改变一下,以使其更“面向对象”并且易于阅读。
public class CreateLeague extends AppCompatActivity {
....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_create_league);
createLeague(...);
}
public void createLeague(final String leagueName, final String username, final String password,final String start,final String end,final String openLeague) {
HttpsTrustManager.allowAllSSL();
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);
}
}
换句话说,我想创建createLeague(...)到另一个类,即CreateLeagueClass,并在上面的类的onCreate()方法中实例化该对象。所以这就是我的工作。
public class CreateLeagueClass extends AppCompatActivity{
private void createLeague(final String leagueName, final String username, final String password,final String start,final String end,final String openLeague) {
HttpsTrustManager.allowAllSSL();
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);
}
}
问题是编译器在这行中给了我一个错误。
Intent i = new Intent(CreateLeague.this, League.class);
CreateLeagueClass的
错误是这样的。
app....CreateLeague is not an enclosing class
有什么建议吗?
谢谢。
答案 0 :(得分:2)
CreateLeague.this
是CreateLeague的当前实例。它只适用于该类,当您将该代码移动到另一个类时,您必须传递该实例,例如在方法CreateLeague cL
中添加参数createLeague
,并在调用该方法时使用CreateLeague.this
。