Reputation: 1836
In my app I'm using Toasts as
Toast.makeText(getBaseContext(), "Updating...", Toast.LENGTH_SHORT).show();
Which has worked fine until now. Now I all of a sudden get the following error for all of my toasts.
Cannot resolve method
getBaseContext()
I found this question on here, which was solved by having the class extend ActionBarActivity
.
The class my toasts are in is the MainActivity.java
and extends AppCompatActivity
, and implements View.OnTouchListener
. I doubt it has anything to do with this, since the class is extending an Activity
anyhow.
I have tried cleaning and rebuilding the project several times. Also, I've done an "Invalidate Caches / Restart..."
Upvotes: 0
Views: 5690
Reputation: 1836
Updating Android Studio did the trick. Sometimes, the solution is so simple.
Upvotes: 1
Reputation: 69
Best practic for using Toast is create a method in class Utils.class with context
public class Utils {
public static void showToast(Context context, String msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
Then u can call this like:
Utils.showToast(context, context.getString(R.string.cannot_connect));
i can update my question if u say that u use fragment or activity for calling Toast method.
on the activity just call this, if not help try with getApplicationContext
in fragments create something like:
private Context context;
private Activity activity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
context = activity.getApplicationContext();
}
But if u not need activity in fragment use:
@Override
public void onAttach(Context context) {
super.onAttach(context);
context = getApplicationContext();
}
also try make context
private Context context
and on OnCreate
context = getApplicationContext();
Upvotes: 0
Reputation: 1469
Try to use this. It worked perfectly for me
Toast.makeText(getApplicationContext(), "Updating...", Toast.LENGTH_SHORT).show();
Try this code too.... Sometimes it would work
Toast.makeText(this, "Updating...", Toast.LENGTH_SHORT).show();
Upvotes: 1
Reputation: 10859
try this
Toast.makeText(YourClass.this.getBaseContext(), "Updating...", Toast.LENGTH_SHORT).show();
or clean build your project
Upvotes: 1
Reputation: 744
use this
Toast.makeText(getApplicationContext(), "your Message", Toast.LENGTH_SHORT).show();
Upvotes: 1