【问题标题】:Class app.victory..... is not an enclosing class类 app.victory ..... 不是封闭类
【发布时间】:2015-08-24 08:14:27
【问题描述】:

我有以下类,我想对其进行一些更改,以使其更加“面向对象”且易于阅读。

   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

有什么建议吗?

谢谢。

【问题讨论】:

    标签: java android class object


    【解决方案1】:

    CreateLeague.this 是 CreateLeague 的当前实例。它仅适用于该类,当您将该代码移动到另一个类时,您必须传递该实例,例如将参数CreateLeague cL 添加到方法createLeague 并在调用该方法时使用CreateLeague.this

    【讨论】:

    • 谢谢先生。我已将您的答案标记为正确的答案:)。
    猜你喜欢
    • 2016-05-14
    • 2020-05-06
    • 2011-07-05
    • 2013-12-13
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    相关资源
    最近更新 更多