Android Toast reports Activity context. this cannot be resolved to a variable, toastcontext. this

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.