Search code examples
androidandroid-activitygoogle-maps-android-api-2android-maps

How can Google's own MapsActivity result in a ClassNotFoundException on a Samsung phone?


I got the strangest error in my Google Play Console yesterday.

I have a button that opens up directions in the Google Map app. Had it for years. Looks like this:

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

Yesterday i got this error log:

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

I cannot for the world understand how this can happen, and i have never seen it before. Anyone knows more than me?

EDIT: Obviously i know that i can put a try/catch around it. That's not my question. I wondered how it is possible, on a Samsung, to get a ActivityNotFound on something in the google maps API. Only thing i can think of is a rooted phone?

This button floats on top of a Google map, and we check for google play services when app is started, so you can't get to this point unless you are already seeing a Google map in the app.


Solution

  • "Able to show a Google map" and "the Google Maps app is installed" are not the same thing. Clearly your user has Play Services installed (used to embed a map in your app), but not Google Maps. Presumably he or she has rooted the phone and installed a different ROM that doesn't have Google Maps. Generally (as you can now see) it's not a good idea to assume anything about what apps will be installed on a phone in the wild.

    Here's my code to check for it:

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