单击菜单项android时强制关闭

时间:2014-06-14 04:18:44

标签: android forceclose rate

我有一个菜单充气器有2个选项,关于和速率。关于工作正常,当点击率时,它会强制关闭。

    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);
            }

和清单代码

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

和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 }

我确信这是一件很简单的事情,我忽略了它,但这让我感到疯狂。再次,关于工作正常,Rate会导致强制关闭而不是将市场URL加载到我的应用程序,以便用户可以对其进行评级。

2 个答案:

答案 0 :(得分:3)

您运行此代码的设备或模拟器可能无法安装 Play商店应用。当您尝试启动系统中没有有效过滤器的Intent时,您将获得这些例外。

另一种方法是在尝试启动意图之前验证这一点(通过PackageManager)。如果没有活动匹配,则使用网址URI(即http://play.google.com/store/apps/details?id=<package_name>)代替。

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

(或者,有点粗糙但更简单,抓住ActivityNotFoundException并做同样的事情。)

答案 1 :(得分:2)

即使没有Play商店,您也可以这样做以确保其有效:

final String appPackageName = getPackageName(); 

try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
}

如果未安装Play商店应用,则会启动浏览器。