2014-06-14 23 views
0

我有一個帶有2個選項的菜單充氣器,About和Rate。關於工作很好,當點擊率時,強制關閉。單擊菜單項時強制關閉android

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 } 

我相信這是一些簡單的,我被忽視,但它讓我瘋狂。同樣,關於工作正常,速度導致一個力量關閉,而不是加載市場的URL到我的應用程序,因此用戶可以評價它。

+0

這是模擬器還是設備? – matiash

回答

3

您正在運行此代碼的設備或模擬器可能沒有安裝Play商店應用程序。當您嘗試啓動系統中沒有有效過濾器的Intent時,您將會遇到這些異常。

另一種方法是在嘗試啓動意圖之前對其進行驗證(通過PackageManager)。如果沒有任何活動匹配,則改爲使用網址(即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,做同樣的)。

+0

我剛剛注意到模擬器上沒有Play商店應用程序。我會嘗試明天在設備上加載應用程序並再次運行。謝謝 – user2727048

+0

@ user2727048是的,那是最可能的解釋:)很高興有幫助。 – matiash

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商店應用這會啓動瀏覽器。

相關問題