Get Activity (this) inside setOnClickListener

always try to use YOUR_ACTIVITY_NAME.this because when you use only "this", it point to the current context . let say you are in OnClickListener , it is an anonymous class so, when you use this inside this. it will point to the button not to the activity. Therefore, you need to point towards activity by using activityname.this.


As you need context and not activity, variable needs to be:

Context homeActivity;

or

Context context;  

Then in onCreate()

context=this; 

use that context or homeActivity in your activity or HomeActivity.this

Inside the click listener, "this" is a reference for the click listener.

Tags:

Java

Android