Google自己的MapsActivity如何在三星手机上产生ClassNotFoundException?

问题描述 投票:0回答:2

我昨天在Google Play Console中收到了最奇怪的错误。

我有一个按钮,可以在Google地图应用中打开方向。好久好久。看起来像这样:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                Uri.parse("http://maps.google.com/maps?f=d&daddr=" +
                    lat + "," + lon));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
            activity.startActivity(intent);

昨天我收到了这个错误日志:

Samsung Galaxy J6+ (j6primelte), Android 8.1
android.content.ActivityNotFoundException: 

我不能让世界了解这是怎么发生的,我以前从未见过它。谁知道比我更多?

编辑:显然我知道我可以尝试/捕捉它。那不是我的问题。我想知道在三星上如何在谷歌地图API中获取一些ActivityNotFound是可能的。我唯一能想到的是根电话吗?

此按钮浮动在Google地图的顶部,我们会在应用启动时检查Google Play服务,因此除非您已在应用中看到Google地图,否则无法达到此目的。

android android-activity google-maps-android-api-2 android-maps
2个回答
1
投票

“能够显示谷歌地图”和“安装谷歌地图应用程序”是不一样的。显然,您的用户已安装Play服务(用于在您的应用中嵌入地图),但不包括Google地图。据推测,他或她已根植手机并安装了一个没有谷歌地图的不同ROM。一般情况下(正如您现在所看到的),假设在野外手机上安装哪些应用程序并不是一个好主意。

这是我的代码来检查它:

public void showDirections(String address, FragmentActivity activity) {

    address = Uri.encode(address);
    Uri gmmIntentUri = Uri.parse("google.navigation:q=" + address);
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
    mapIntent.setPackage("com.google.android.apps.maps");

    if (mapIntent.resolveActivity(activity.getPackageManager()) == null) {
        DialogUtils.getInstance(activity).reportError(activity, "Google Maps is required for this feature");
        return;
    }

    activity.startActivity(mapIntent);
}

1
投票

确保你的代码,

try {

    //Put your code here

    } catch (Exception e) {
        e.printStackTrace();
    }

因为当您设置打开特定应用程序的意图时,可能该应用程序未安装在该设备中(在您的情况下是三星手机),因此在低于calss的情况下,它会抛出异常。

intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
© www.soinside.com 2019 - 2024. All rights reserved.