简体   繁体   中英

How to handle exceptions in super.onCreate?

Is there a safe way to handle an exception thrown by super.onCreate(savedInstanceState) ?

A very small number of my users are hitting this error

java.lang.NoClassDefFoundError - android.security.MessageDigest

The causes are discussed in this question , but basically onCreate for the MapActivity class can throw a NoClassDefFoundError . I'm concerned that detecting the condition in advance may not be future proof.

I thought the safest solution for my class (which extends MapActivity) would be to catch the NoClassDefFoundError and launch a URL to Google maps instead, eg

    try {            
        super.onCreate(savedInstanceState);
    } catch (NoClassDefFoundError err) {
        StringBuilder url = new StringBuilder(
                "http://maps.google.com/maps?q=").append(latitude)
                .append(",").append(longitude).append("&z=18");

        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url.toString()));
        startActivity(i);
        finish();
        return;
    }

However, I'm worried that my code will crash with a SuperNotCalledException when the NoClassDefFoundError occurs.

Is there a safe way to handle exceptions/errors in a call to super.onCreate?

A very small number of my users are hitting this error

They are users running pirated versions of the Google Maps subsystem. Google Maps works just fine on all versions of Android, and licensees will get a proper version. However, mentally-challenged pirates will sometimes use an old pirated set of Maps binaries on a newer device, leading to this issue.

However, I'm worried that my code will crash with a SuperNotCalledException when the NoClassDefFoundError occurs.

There is a good chance that this will indeed be the case, or some other exception, as startActivity() may crash because of the missing super.onCreate() . So might finish() , for that matter. You are certainly welcome to try it, though.

Is there a safe way to handle exceptions/errors in a call to super.onCreate?

Not generally.

MapActivity has been deprecated in favor of MapFragment, which is part of Google Play Services. This new API is backwards compatible, I think.

I suggest you do prior detection of MapActivity. It will be future-proof, because the MapActivity API is not going to change. The MapFragment API requires the Google Play Services APK; the documentation shows you how to verify that it's present and prompt the user if it isn't. The APK is available from Google Play Store, and is made available by OTA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM