找不到处理Intent的Activity

cla*_*n33 38 android android-intent

我试图发起一个打开Android市场链接的意图.

android manifest部分看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

启动意图如下:

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


                }
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

Com*_*are 84

您在缺少Google Play商店的Android环境中运行此代码,例如模拟器,Kindle Fire等.

如果您在模拟器上遇到此问题,请在具有Play商店的设备上测试此代码路径.

如果您在缺少Play商店的某些硬件上遇到此问题,或者您计划将应用程序分发给缺少Play商店的设备,请处理异常或使用PackageManagerresolveActivity()确定您的Intent呼叫是否成功startActivity().

if(intent.resolveActivity(getPackageManager()) != null)
    startActivityForResult(intent, 0);
else
    ...
Run Code Online (Sandbox Code Playgroud)

  • shooootttt,我试图在模拟器上开放市场:// url,因为它没有市场:( Phonegap 3.4 (2认同)

Ole*_*Ole 17

您可以使用Intent.createChooser()方法安全地启动Intent.如果不存在可以处理您的意图的应用程序,则会显示一个对话框,告诉用户这一点.但是,如果安装了Google Play商店,则用户可以选择在Play商店中"打开"该商标.

startActivity(Intent.createChooser(marketIntent, "dialogTitle"));
Run Code Online (Sandbox Code Playgroud)


loc*_*ost 13

更好的解决方案是尝试在Google Play应用中打开uri,但如果没有这样的应用程序(没有Activity来处理此意图) - 您可以尝试在浏览器中打开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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Nar*_*ren 7

尝试添加

<category android:name="android.intent.category.DEFAULT" />
Run Code Online (Sandbox Code Playgroud)

到调用Activity.