toast - getContext () , getApplicationContext(), getBaseContext are not working in Android -
i want show toast
message getcontext()
in toast.maketext((getcontext()," message" , toat.length_long.show()))
giving error
cannot resolve method.
the problem in class want show toast
message not mainactivity
class. asynctask
class. can show toast
message in other classes (other mainactivity
class) above mentioned problem?
import android.os.asynctask; import android.widget.toast;
public class myclass extends asynctask<string, string, string> { public myclass(double a, double b,context context ) { this.a = a; this.b=b; this.context = context; } protected string doinbackground(string... params) { return null; } protected void onpostexecute(string result) { toast.maketext((getapplicationcontext(), "message", toast.length_long).show(); } }
edit made constructor (see above code) in mainactivity
class calling in way myclassobj = new myclass(a, b,this);
giving error
myclas() in myclass cannot applied to: expected actual parameters arguments a: double b: double b context: android.content.context this(anonymous...view.view.onclicklistener)
edit3
import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; import android.view.menu; import android.view.menuitem; public class mainactivity extends appcompatactivity { myclass object; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { double age = 16; double number = 33; object = new myclass(age,number,this); } }); } }
secondclass.
import android.content.context; import android.os.asynctask; import android.widget.toast; public class myclass extends asynctask<string, string, string> { context context; double a; double b; public myclass(double a, double b,context context ) { this.a = a; this.b=b; this.context = context; } protected string doinbackground(string... params) { return null; } protected void onpostexecute(string result) { toast.maketext((context), "message", toast.length_long).show(); } }
when using this
refers enclosing class. in case view.onclicklistener
. need pass context of activity.
so need call way,
object = new myclass(age,number, mainactivity.this);
Comments
Post a Comment