Android Toast reports Activity context. this cannot be resolved to a variable, toastcontext. this
For android development, I insert a button in my activity. If the andriod market cannot be found, I will use toast for prompts. Some problems occurred while using it. The following code reports an error.Context cannot be resolved to a variable
1234567 |
Uri uri = Uri.parse( "market://details?id=" + getApplicationContext().getPackageName()); Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); try { startActivity(goToMarket); } catch (ActivityNotFoundException e) { Toast.makeText(Activity. this , "Couldn't launch the market" , Toast.LENGTH_LONG).show(); } |
I tried the following call:
Toast. makeText (this, "Couldn't launch the market", Toast. LENGTH_LONG). show ();
Then The development tool prompts the method makeText (Context, CharSequence, int) in the type Toast is not applicable for The arguments (new View. OnClickListener () {}, String, int)
I used to use it this way, but I don't understand how it works.
Solution
If you inherit the Activity, use the following two methods:
Toast. makeText (ClassName. this, "Couldn't launch the market", Toast. LENGTH_LONG). show ();
Or
Toast. makeText (getApplicationContext (), "Couldn't launch the market", Toast. LENGTH_LONG). show ();
If your class inherits Fragment, use the following method:
Toast. makeText (getActivity (), "Couldn't launch market", Toast. LENGTH_LONG). show ();
Address: http://www.itmmd.com/201411/153.html
This article is organized and published by Meng IT personnel. The reprinted article must indicate the source.