Instead of this
, try getApplicationContext
or getBaseContext
.
this
in this example does nor refer to the context
.
Question
As the onPostExecute of an AsyncTask runs on the UIthread, it should be possible to add a PopupMenu in it. But when I do so, I get a compiler error that I don't know how to fix:
The constructor PopupMenu(My.....dothedirectionTHREAD, View) is undefined
The line of code is:
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.reset));
What am I missing?
Solution
Instead of this
, try getApplicationContext
or getBaseContext
.
this
in this example does nor refer to the context
.
OTHER TIPS
this
refers to AsyncTask but you need Activity context. Try below or your Activity class.
PopupMenu popupMenu = new PopupMenu(MainActivity.this, findViewById(R.id.reset));
In this line
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.reset));
the this
pointer is referring to your AsyncTask class, which is not valid as a Context.
Assuming your AsyncTask is an inner class in your Activity, you should use something like this:
PopupMenu popupMenu = new PopupMenu(MyActivity.this, findViewById(R.id.reset));
I would guess, that you have to use your appcontext instead of "this" which delivers in your case the Thread and not the context.
PopupMenu popupMenu = new PopupMenu(context, findViewById(R.id.reset));
https://developer.android.com/reference/android/widget/PopupMenu.html