2

有没有一种安全的方法来处理抛出的异常super.onCreate(savedInstanceState)

我的一小部分用户遇到了这个错误

java.lang.NoClassDefFoundError - android.security.MessageDigest

原因在this question中讨论过,但基本上onCreate对于MapActivity类可以抛出一个NoClassDefFoundError。我担心提前检测到这种情况可能不是未来的证据。

我认为我的班级(扩展 MapActivity)最安全的解决方案是捕获NoClassDefFoundError并启动一个指向 Google 地图的 URL,例如

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

但是,我担心我的代码会SuperNotCalledExceptionNoClassDefFoundError发生时崩溃。

在对 super.onCreate 的调用中是否有一种安全的方法来处理异常/错误?

4

2 回答 2

3

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.

于 2013-03-14T23:28:07.570 回答
0

MapActivity 已被弃用,取而代之的是 MapFragment,它是 Google Play 服务的一部分。我认为这个新的 API 是向后兼容的。

我建议您事先检测 MapActivity。它将是面向未来的,因为 MapActivity API 不会改变。MapFragment API 需要 Google Play Services APK;该文档向您展示了如何验证它是否存在并在不存在时提示用户。APK 可从 Google Play 商店获得,并由 OTA 提供。

于 2013-03-14T22:49:31.783 回答