Android ActivityNotFoundException处理

Android ActivityNotFoundException处理,android,android-intent,exception-handling,Android,Android Intent,Exception Handling,我已经有几年没有做过异常处理了,我似乎不知道自己做错了什么。我有一个应用程序可以打开我的另一个应用程序,如果用户还没有其他应用程序,我希望当前应用程序重定向到Play Store。现在,如果应用程序未安装,程序将强制关闭。我目前的代码是: try{ Intent intent = new Intent(); PackageManager manager = getPackageManager(); intent = manager.getLaunchIntentForPa

我已经有几年没有做过异常处理了,我似乎不知道自己做错了什么。我有一个应用程序可以打开我的另一个应用程序,如果用户还没有其他应用程序,我希望当前应用程序重定向到Play Store。现在,如果应用程序未安装,程序将强制关闭。我目前的代码是:

try{
    Intent intent = new Intent();
    PackageManager manager = getPackageManager();
    intent = manager.getLaunchIntentForPackage("my.app.package");
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(intent);
   }
catch(ActivityNotFoundException activityNotFound){
    Intent playStoreIntent = new Intent(Intent.ACTION_VIEW, 
    Uri.parse("market://details?id=my.app.package"));
    startActivity(playStoreIntent);
   }

我有一种感觉,我正在做一些非常愚蠢的事情,并且错误地使用了
catch
函数。任何帮助都将不胜感激

您可以尝试以下操作:不需要try catch

 Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(packagename);

 if(null!=LaunchIntent)
    {
       startActivity(LaunchIntent);
    }
else
    {
      //intent to open the market.
    }

您可以尝试这样做:不需要try catch

 Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(packagename);

 if(null!=LaunchIntent)
    {
       startActivity(LaunchIntent);
    }
else
    {
      //intent to open the market.
    }

请尝试使用此代码。评论中给出的解释

String packageName = "my.app.package";
try{
        Intent intent = new Intent();
        PackageManager manager = getPackageManager();
        intent = manager.getLaunchIntentForPackage(packageName);
        //if application not installed, intent to get launcher will be null
        if(intent != null) {
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            startActivity(intent);
        }else{
                   //launch play store with package name
            Intent playStoreIntent = new Intent(Intent.ACTION_VIEW, 
                    Uri.parse("market://details?id=my.app.package"));
                    startActivity(playStoreIntent); 
        }
       }
    catch(ActivityNotFoundException activityNotFound){
        // to handle play store not installed scenario
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                .parse("http://play.google.com/store/apps/details?id=" + packageName));
        startActivity(intent);
}

请尝试使用此代码。评论中给出的解释

String packageName = "my.app.package";
try{
        Intent intent = new Intent();
        PackageManager manager = getPackageManager();
        intent = manager.getLaunchIntentForPackage(packageName);
        //if application not installed, intent to get launcher will be null
        if(intent != null) {
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            startActivity(intent);
        }else{
                   //launch play store with package name
            Intent playStoreIntent = new Intent(Intent.ACTION_VIEW, 
                    Uri.parse("market://details?id=my.app.package"));
                    startActivity(playStoreIntent); 
        }
       }
    catch(ActivityNotFoundException activityNotFound){
        // to handle play store not installed scenario
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                .parse("http://play.google.com/store/apps/details?id=" + packageName));
        startActivity(intent);
}

你可以发布你的logcat异常吗?在删除
intent.addCategory(intent.CATEGORY\u启动器)后尝试getLaunchIntentForPackage()返回null,因此您在调用空意图的addCategory(…)时得到NullPointerException。您可以发布日志猫异常吗?请在删除
intent.addCategory(intent.CATEGORY\u LAUNCHER)后重试getLaunchIntentForPackage()返回null,因此您在调用addCategory(…)时会收到NullPointerException,该意图为null。我注意到有些人没有安装Play Store,导致ActivityNotFoundException。谁没有安装Play Store?奇怪的感谢您提供在浏览器中打开它的代码。我注意到有些人没有安装Play Store,导致ActivityNotFoundException。谁没有安装Play Store?奇怪的感谢您提供在浏览器中打开它的代码。