簡體   English   中英

找不到處理 Intent 的活動

[英]No Activity found to handle Intent

我正試圖啟動一個打開 android 市場鏈接的意圖。

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

日志輸出:

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)

您在缺少 Google Play 商店的 Android 環境(例如模擬器、Kindle Fire 等)上運行此代碼。

如果您在模擬器上遇到此問題,請在具有 Play 商店的設備上測試此代碼路徑。

如果您在缺少 Play Store 的某些硬件上遇到此問題,或者如果您計划將應用程序分發到缺少 Play Store 的設備,請處理異常或使用PackageManagerresolveActivity()來確定您的Intent是否會在調用startActivity()之前成功。

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

您可以使用 Intent.createChooser() 方法來安全地啟動 Intent。 如果不存在可以處理您的意圖的應用程序,將顯示一個對話框,告訴用戶這一點。 但是,如果安裝了 Google Play Store,則用戶可以選擇在 Play Store 中“打開”intent。

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

更好的解決方案是嘗試在 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);
    }
}

嘗試添加

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

到調用活動。

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

    }

我得到了同樣的錯誤,因為我寫道:

Linking.openURL('www.somewebsite.com');

將 URL 更改為http://somewebsite.com (或https://somewebsite.com ,具體取決於您定位的網站)解決了問題:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM