android - Calling method of another activity from Asynctask -


i getting syntax error - "no enclosing instance of type mainfragmentactivity accessible in scope" in method buildreport -

enter image description here

buildreport method -

public void buildreport(view v) {     //syntax error here     new verifydialogue(mainfragmentactivity.this).execute(daterange);  } 

this asynctask class -

class verifydialogue extends asynctask<string, string, string> { public mainfragmentactivity activity;  public verifydialogue(mainfragmentactivity a) {     //this how getting instance of activity     this.activity = a;  }  @override protected void onpreexecute() {     super.onpreexecute();     pdialog = new progressdialog(getactivity());     pdialog.setmessage("initializing please wait");     pdialog.settitle("loading");     pdialog.setcancelable(true);     pdialog.show(); }   protected string doinbackground(string... args) {     return ""; }  protected void onpostexecute(string result) {     //there records in sync queue     alertdialog.builder alert =              new alertdialog.builder(     reportstypeactivity.this);     alert.settitle("user records!");     alert.setmessage("you have records on server.");      alert.setpositivebutton("fix",              new dialoginterface.onclicklistener() {     public void onclick(             dialoginterface dialog,              int whichbutton)              {       dialog.cancel();          //calling method of activity mainfragmentactivity         activity.nonsyncscreen();      }     });      alertdialog alertdialog = alert.create();     alertdialog.show();                  } 

you can use mainfragmentactivity.this in nested class. "no enclosing instance of type mainfragmentactivity accessible in scope" means method buildreport(view v) not in nested class inside of mainfragmentactivityclass

possible solution, pass instance of mainfragmentactivity constructor of reportstypeactivity

another possible solution:

mainfragment class:

{      private static mainfragmentactivity instance;      public oncreate(...){         instance = this;     }      public static mainfragmentactivity getinstance(){         return instance;     } } 

reportstypeactivity-class:

public void buildreport(view v) {     //syntax error here     new verifydialogue(mainfragmentactivity.getinstance()).execute(daterange);  } 

edit: getinstance() should static


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -