有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检查NoClassDefFoundError?

在我的Android手机应用程序中,只有三星和QMobile的特定设备出现了一些NoClassDefFoundError错误

添加下面的代码会检测到错误吗

  //Check for Class not found error for Samsung 4.2.2 devices case
  try {
      Class.forName("安卓.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而不导致应用程序崩溃的最佳方法是什么

Edit:

documentation表示该方法抛出以下异常,并且它没有提到任何关于NoClassDefFoundError的内容

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

我可以把这个问题改成这样

Will Class.forName(String)throw NoClassDefFoundError or How can I check if a class has a "Def"?


共 (1) 个答案

  1. # 1 楼答案

    我认为,try-catch是处理异常(本质上是什么)的最佳方法

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

    如果像你说的那样发生在图书馆里,那么你可以做两件事:

    • 向维护人员报告(如果您觉得这确实是他们的错误),或者
    • 尝试修复原因而不是结果(例外)NoClassDefFoundError通常表示您缺少库所需的内容;可能是对另一个库的另一个依赖,或者是其他什么

    试着深入挖掘图书馆的文档,你可能会从中发现一些东西