0

here's a portion of my code:

public class Login  extends Activity {

private class LoginUser extends AsyncTask<String, String, Boolean> {
@Override
        protected void onPostExecute(Boolean returnResult) {
            DialogSelectAccount dsa=new DialogSelectAccount(getParent());
             dsa.show();

      }
   }
}

public class DialogSelectAccount extends Dialog implements android.view.View.OnClickListener  {


    public DialogSelectAccount(Activity a) {
        super(a);
    }
}

but when I run the app, it get a NPE error at the "super(a)" under the public DialogSelectAccount();

but when I changed my code to

public class Login  extends Activity {

private class LoginUser extends AsyncTask<String, String, Boolean> {
@Override
        protected void onPostExecute(Boolean returnResult) {
            test();

      }
   }

public void test(){
        DialogSelectAccount dsa=new DialogSelectAccount(this);
        dsa.show();
    }
}

it works. So what if I don't want to create a separate method like above and calls DialogSelectAccount directly inside the onPostExecute, what should I pass as the argument?

Thanks

imin
  • 4,504
  • 13
  • 56
  • 103

2 Answers2

2

So what if I don't want to create a separate method like above and calls DialogSelectAccount directly inside the onPostExecute, what should I pass as the argument?

answer:

DialogSelectAccount dsa=new DialogSelectAccount(Login.this);

This is rather general java question, for more on inner classes read here: Getting hold of the outer class object from the inner class object

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
0

The dialog class needs a Context attribute. When you say getParent() - I suppose it does not return context.

You can keep the context attribute in a global class and retrieve it - though I will not recommend that.

Varun Verma
  • 542
  • 4
  • 17