无活动处理意向无活动处理意向(No Activity found to handle Intent)

2019-05-11 21:19发布

我试图发动意图打开链接到Android Market。

Android清单部分看起来是这样的:

<activity android:name="com.surreall.sixdice.Start" android:label="Six Dice" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation">
    <intent-filter>
                <action android:name="android.intent.action.MAIN" />                               
                <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
                <action android:name="android.intent.action.VIEW" />  
                <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

启动意图的样子:

public void onClick(View v) {
        String PublisherID = "pub:surreallgames";
        Uri marketUri = Uri.parse("market://search?q="+PublisherID);
        Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);                    
        startActivity(marketIntent);


                }

logcat的输出:

06-17 17:38:47.393: E/AndroidRuntime(476): FATAL EXCEPTION: main
06-17 17:38:47.393: E/AndroidRuntime(476): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://search?q=pub:surreallgames }
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.app.Activity.startActivityForResult(Activity.java:2827)
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.app.Activity.startActivity(Activity.java:2933)
06-17 17:38:47.393: E/AndroidRuntime(476):  at com.surreall.sixdice.Start$9.onClick(Start.java:265)
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.view.View.performClick(View.java:2485)
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.view.View$PerformClick.run(View.java:9080)
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.os.Handler.handleCallback(Handler.java:587)
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.os.Looper.loop(Looper.java:123)
06-17 17:38:47.393: E/AndroidRuntime(476):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-17 17:38:47.393: E/AndroidRuntime(476):  at java.lang.reflect.Method.invokeNative(Native Method)
06-17 17:38:47.393: E/AndroidRuntime(476):  at java.lang.reflect.Method.invoke(Method.java:507)
06-17 17:38:47.393: E/AndroidRuntime(476):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-17 17:38:47.393: E/AndroidRuntime(476):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-17 17:38:47.393: E/AndroidRuntime(476):  at dalvik.system.NativeStart.main(Native Method)

Answer 1:

你是对缺乏谷歌Play商店,如仿真器,Kindle Fire的,等一个Android环境中运行该代码

如果您遇到此模拟器上进行,测试有Play商店的设备上此代码路径。

如果您遇到此对一些硬件的缺乏Play商店,或者如果您对您的应用分发给缺乏Play商店的设备规划,无论是处理异常或使用PackageManagerresolveActivity()以确定您的Intent会打电话之前成功startActivity()

if(intent.resolveActivity(getPackageManager()) != null)
    startActivityForResult(intent, 0);
else
    ...


Answer 2:

您可以使用Intent.createChooser()方法来安全地启动意图。 如果没有应用程序都可以处理你的意图,将显示一个对话框,只是告诉用户。 然而,如果谷歌Play商店安装,用户可那些选择“打开”,在Play商店的意图。

startActivity(Intent.createChooser(marketIntent, "dialogTitle"));


Answer 3:

更好的解决办法是尝试在谷歌打开URI Play应用程序,但是,如果没有这样的应用程序(不活动来处理这个意图) - 你可以尝试在浏览器中打开的URI就像这个例子:

public static void rateApp(Context context) {
    try {
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
    } catch (android.content.ActivityNotFoundException anfe) {
        viewInBrowser(context, "https://play.google.com/store/apps/details?id=" + context.getPackageName());
    }
}

public static void viewInBrowser(Context context, String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    if (null != intent.resolveActivity(context.getPackageManager())) {
        context.startActivity(intent);
    }
}


Answer 4:

尝试添加

<category android:name="android.intent.category.DEFAULT" />

到调用活动。



Answer 5:

private void OpenStoreIntent(){
        String url="";
        Intent storeintent=null;
        try {
            url = "market://details?id=com.myapp.packagename";
            storeintent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            storeintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            context.startActivity(storeintent);
        } catch ( final Exception e ) {
            url = "https://play.google.com/store/apps/details?id=com.myapp.packagename";
            storeintent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            storeintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            context.startActivity(storeintent);
        }

    }


文章来源: No Activity found to handle Intent