Sabarish
Sabarish

Reputation: 1184

Access a method from another method within same class - android

In a Class MyClass, I have a method

public static void myMethod1(Context context, ClassA a, final ClassB.CompletedTaskListener completedListener) throws JSONException, SQLException {

I need to access the above method inside the below method of the same class,

private static void myMethod2(final MyClass myclass) throws JSONException { 
    User currentUser = MainActivity.getUser();
    final HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("username", myclass.getUsername());
    params.put("data", CommonUtils.toJsonString(myclass.toMap()));
    try {
        HttpClientUtils.postSignedRequestWithCallback(URL, params,
            APISignatureType.SIG_COMPLEX, 
            new HttpClientUtils.CustomCallbackListener() {
                @Override
                public void onSuccess(JSONObject jsonObject) {
                    myclass.setSaved(true);
                    myclass.setStatus(AppStatus.STATUS_SUBMITTED);
                    //Here I use the myMethod1 which shows error
                    myclass.myMethod1(context,user,completedOneDownloadTaskListener);
                }
                @Override
                public void onFailure(int statusCode) {
                }
            });
    }catch (Exception e){
    }
} 

When i use myMethod1(context,a,completedListener); inside the myMethod2 I get compilation error.

I get error as,

Cannot resolve symbol 'completedListener' 
Cannot resolve symbol 'a'
Non-static field 'context' cannot be referenced from a static context Static member 'com.mypack.model.MyClass.myMethod(android.content.Context, com.mypack.model.ClassA, com.mypack.service.ClassB.CompletedTaskListener)' accessed via instance reference

Upvotes: 0

Views: 396

Answers (2)

milcaepsilon
milcaepsilon

Reputation: 272

In complement to Bonatti answer I suggest to put an other parameter in your method2 so you can still use your static methods since context is not accessed via instance reference.

private static void myMethod2(final MyClass myclass, Context context) throws JSONException

Upvotes: 0

Bonatti
Bonatti

Reputation: 2781

The context is not a static value, but rather an object.

Either remove the static modifier from your methods, or dont use context inside them

Upvotes: 1

Related Questions