Question

I'm trying to insert a button to rate app in my activity, with a toast for if market isn't found. But I'm getting a "Context cannot be resolved to a variable" on Activity.this:

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've also tried:

Toast.makeText(this, "Couldn't launch the market", Toast.LENGTH_LONG).show();

But then I get Multiple markers at this line - The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)

I've made a simple button toast the same way (without try/catch) before, and then it worked fine.. What have I done wrong?

Was it helpful?

Solution

If your class is extending with Activity means use like this

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 is extending with Fragment means use like this:

Toast.makeText(getActivity(), "Couldn't launch market",Toast.LENGTH_LONG).show();

OTHER TIPS

Your answer:

Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LENGTH_LONG).show();

Try:

Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LEGTH_LONG).show();

Try this...

Uri uri = Uri.parse("market://details?id="
                + getApplicationContext().getPackageName());
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            startActivity(goToMarket);

        } catch (ActivityNotFoundException e) {
            this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't launch the market", Toast.LENGTH_LONG)
                            .show();
                }
            });
        }

Hope this will help you...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top