0

Icreated some AsyncTask classes but I created them separated from activity class.

The issue with onPostExecute(..):

public class UserLoginTask extends AsyncTask<Void, Void, User> {

    private Context mContext;
.....

    @Override
    protected void onPostExecute(final User registedUser) {
        mAuthTask = null;
        mloginHelper.showProgress(false,mLoginFormView,mProgressView);
        if (registedUser == null) {
            mloginHelper.registerUser(mEmail, mPassword);
        } else if (registedUser.getPassword().equals(mPassword)) {
            mContext.getApplicationContext().setLoggedUser(registedUser, mRememberMe);
            mContext.finish();
        } else {
            mPasswordView.setError(mContext.getString(R.string.error_incorrect_password));
            mPasswordView.requestFocus();
        }
    }
.....
}

All the methods that related to member objects of AsyncTask can't be resolved inside it.

Marzouk
  • 2,650
  • 3
  • 25
  • 56
  • For example,which method can't be resolved? – starkshang Mar 13 '16 at 04:00
  • Do you know why it's final ? – Shree Krishna Mar 13 '16 at 04:17
  • In Short Here is the solution: [How to get the result of onpostexecute() to main activity when the AsyncTask class is not an inner class of the activity.](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a?lq=1) – Marzouk Mar 15 '16 at 13:14

2 Answers2

1

you have to typecast the context object to COAApplication.

((COAApplication)mContext).getApplicationContext().setLoggedUser(registedUser, mRememberMe);

EDIT

you won't be able to access activity variables outside the class. You can achieve this in a different way. You can follow interface approach. Create a interface with a method, implement it in your activity class pass the reference of your activity to the asynctask. onPostExecute(), just call the interface method.

This way you will be able to send a callback to the activity and you can perform these operations inside the activity only.

Ankit Aggarwal
  • 5,317
  • 2
  • 30
  • 53
0

It's because getApplicationContext get an ApplicationContext Object,while you need a COAApplication Object,so you should cast the result to this type,like this:

((COAApplication)mContext).getApplicationContext().setLoggedUser(registedUser, mRememberMe);

Generally,we usually define a method in our Application Class to get singleton of the class,like this:

public class COAApplication extends Application{
    private static COAApplication mSingleton = null;
    protected void onCreate(){
        mSingleton = this;
    }

    public static COAApplication getSingleton(){
        return mSingleton;
    }

then in everywhere you need this class,just call COAApplication.getSingleton() and you don't need to cast type,which make code ugly.

starkshang
  • 8,228
  • 6
  • 41
  • 52
  • Thanks for your answer but i think that the issue not specifically with getApplicationContext, check my last update on the question. – Marzouk Mar 13 '16 at 03:51