Skip to content Skip to sidebar Skip to footer

Toast.maketext(getapplicationcontext(), "string", Toast.length_long); ==>here Getapplicationcontext() Cannot Change To "this"?

First the format of Toast.makeText(): public static Toast makeText (Context context, CharSequence text, int duration) the first argument is Context, the function getApplicationCont

Solution 1:

When you create a new OnClickListener, you are creating a anonymous class which implements a particular interface. Thus, this does not refer to the Activity, since you are actually in another object.

Here's some more info on the subject Anonymous classes vs delegates

Solution 2:

Iin this case this is indicating OnClickListener instance, to create view, or other UI stuff, you need to get context, this can be done by following different methods:

getApplicationContext();
ContextMenuResourcesActivity.this;
v.getContext();

Solution 3:

new OnClickListner() is an anonymous class that implements onclick interface and this refers to the instance of the anonymous class. Rather use "Your_Activity_Name.this" to refer to the current context of your activity.

Post a Comment for "Toast.maketext(getapplicationcontext(), "string", Toast.length_long); ==>here Getapplicationcontext() Cannot Change To "this"?"