这个/扩展上下文?

时间:2014-09-25 16:58:37

标签: android rest this android-volley android-context

我无法在此代码中使用(this)。我从IDEA得到的建议是扩展Context,我绝对不想这样做。我尝试导入Context并使用(Context context),但没有运气。为什么我不能用“这个”?

公共类LastFMLogin {

final String lastFMKeyURL = "http://www.last.fm/api/auth/?api_key=" + R.string.lastfm_api_key;
RequestQueue queue = Volley.newRequestQueue(this);

public void authGetRequest() {
    // prepare the Auth Get Request
    JsonObjectRequest lastFMAuthRequest = new JsonObjectRequest(Request.Method.GET, lastFMKeyURL, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // display response
                    Log.d("Response", response.toString());
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", error.toString());
                }
            });

    // add it to the RequestQueue
    queue.add(lastFMAuthRequest);

}

}

我的Tutorial类调用lastFMAuthRequest,但是我收到“无法解决上下文”错误。

public void lastFMButtonListener() {

    ImageButton lastfm_login = (ImageButton) findViewById(R.id.lastfm_login);
    lastfm_login.setOnClickListener(new OnClickListener() {
            @Override
        public void onClick(View view) {

            Log.d(debug, "LastFM Round clicked");
            //Change button image
            ImageButton lastfm_login = (ImageButton) findViewById(R.id.lastfm_login);
            lastfm_login.setImageResource(R.drawable.lastfm_pressed);
            LastFMLogin lastFMLogin = new LastFMLogin();
            lastFMLogin.lastFMAuthRequest(Context context);
        }
    });
}

3 个答案:

答案 0 :(得分:2)

你可以使用&#34;这个&#34;在服务或活动中。等,但不是在自定义类。因为&#34;这个&#34;在自定义(不扩展)类不等于上下文。使用satti代码为您的班级提供背景信息。


或仅为方法提供上下文:

final String lastFMKeyURL = "http://www.last.fm/api/auth/?api_key=" +  R.string.lastfm_api_key;
RequestQueue queue = null;

 public void authGetRequest(Context context) {

 queue = Volley.newRequestQueue(context);

// prepare the Auth Get Request
JsonObjectRequest lastFMAuthRequest = new JsonObjectRequest(Request.Method.GET, lastFMKeyURL, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                // display response
                Log.d("Response", response.toString());
            }
        },

        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Error.Response", error.toString());
            }
        });

// add it to the RequestQueue
queue.add(lastFMAuthRequest);

}

在许多方法中使用Context时,默认构造函数的使用是很好的,并且在调用它时不希望在每个方法中添加Context param。 例如:

MyClass myClass = new MyClass(this); // Called ones

然后只使用:

myClass.method1();
myClass.method2();
myClass.method3();
myClass.method4();

每个方法都使用您在默认构造函数中提供的Context。

此外,你不仅可以使用&#34;这个&#34;。

getApplicationContext()
getBaseContext()

文档中解释的差异。

享受! )

答案 1 :(得分:1)

创建一个构造函数,它将接受Context,并在任何你想要的地方使用它。

例如:

public class LastFMLogin {
    private Context mContext;

    public LastFMLogin(Context context) {
        mContext = context;
    } 
}

答案 2 :(得分:0)

因为在这种情况下,'this'指的是作为方法(接口实现)的param创建的Anonymous#InnerClass。

您可以使用FullName.this

引用外部类

public void lastFMButtonListener(){

ImageButton lastfm_login = (ImageButton) findViewById(R.id.lastfm_login);
lastfm_login.setOnClickListener(new OnClickListener() {
        @Override
    public void onClick(View view) {

        Log.d(debug, "LastFM Round clicked");
        //Change button image
        ImageButton lastfm_login = (ImageButton) findViewById(R.id.lastfm_login);
        lastfm_login.setImageResource(R.drawable.lastfm_pressed);
        LastFMLogin lastFMLogin = new LastFMLogin();
        lastFMLogin.lastFMAuthRequest(MyClass.this);
    }
});

}