1

我的 Android 移动应用程序中很少有 NoClassDefFoundError 仅来自三星和 QMobile 的特定设备。

添加以下代码会检测到错误吗?

  //Check for Class not found error for Samsung 4.2.2 devices case
  try {
      Class.forName("android.support.v7.internal.view.menu.MenuBuilder");
  } catch (ClassNotFoundException e) {
      // Handle Exception by displaying an alert to user to update device firmware.
  }catch (NoClassDefFoundError e){
      // Handle Exception by displaying an alert to user to update device firmware.
  }

主动检测 NoClassDefFoundError 而不会导致应用程序崩溃的最佳方法是什么?

编辑:

文档说该方法引发了以下异常,并且没有提到任何关于NoClassDefFoundError.

Throws:
LinkageError - if the linkage fails
ExceptionInInitializerError - if the initialization provoked by this method fails
ClassNotFoundException - if the class cannot be located

我可以改写这个问题

Class.forName(String)抛出NoClassDefFoundError或如何检查一个类是否有“Def”?

4

1 回答 1

0

我认为 try-catch 是处理异常的最佳方式(NoClassDefFoundError本质上就是这样)。

但是处理导致这种情况发生的事情而不是仅仅遵守结果可能是明智的;NoClassDefFoundError是一种效果,您必须采取一些措施来防止它突然出现。

如果如您所说,它发生在图书馆内,那么您可以做两件事:

  • 向维护者报告(如果你觉得这确实是他们的错误),或者
  • 尝试解决原因而不是结果(异常)。NoClassDefFoundError通常表明您缺少图书馆需要的东西;无论是对另一个库的另一个依赖还是其他东西。

尝试更深入地研究图书馆的文档,您可能会从中找到一些东西。

于 2016-03-08T11:20:01.817 回答