Search code examples
androidforcecloserate

force close when clicking menu item android


I have a menu inflator with 2 options, About, and Rate. About works just fine, when rate is clicked, it force closes.

    case R.id.About:
    Intent i = new Intent(this, About.class);
    startActivity(i);
    break;
    case R.id.Rate:
    Intent marketIntent = new Intent(Intent.ACTION_VIEW,
               Uri.parse("market://details?
                     id="+"com.androidsleepmachine.gamble"));
        startActivity(marketIntent);
            }

and the manifest code

<activity android:name="com.androidsleepmachine.gamble.About"/>
    <activity android:name="com.androidsleepmachine.gamble.Rate" />

and the Logcat

06-13 23:59:30.294: E/AndroidRuntime(1576): FATAL EXCEPTION: main
06-13 23:59:30.294: E/AndroidRuntime(1576): android.content.ActivityNotFoundException:   
No Activity found to handle Intent { act=android.intent.action.VIEW 
dat=market://details?id=com.androidsleepmachine.gamble }

I am sure it is something simple that I have overlooked but it is driving me insane. Again, About works just fine, Rate causes a force close instead of loading the marketplace URL to my app so the user can rate it.


Solution

  • It's possible that the device or emulator where you're running this code doesn't have the Play Store app installed. When you try to start an Intent for which there is no valid filter in the system, you will get these exceptions.

    An alternative would be to verify this (via PackageManager) before trying to start the intent. If no activity matches, then use the web URI (i.e. http://play.google.com/store/apps/details?id=<package_name>) instead.

    List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(marketIntent, 0);
    if (apps.size() != 0)
       <use market intent>
    else
       <use http intent>
    

    (Or, a bit cruder but simpler, catch ActivityNotFoundException and do the same).