错误:java.lang.NoClassDefFoundError:解析失败:Lorg / apache / xmlbeans / XmlOptions;

时间:2017-09-19 09:50:30

标签: java android excel apache lib

我想用Apache将数据写入Excel,我在这行收到错误 XSSFWorkbook工作簿=新的XSSFWorkbook();我在下面发布了我的错误日志请检查。

 Process: app.msupply.com.ideaurben, PID: 28508
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/xmlbeans/XmlOptions;
at org.apache.poi.POIXMLDocumentPart.<clinit>(POIXMLDocumentPart.java:53)
at app.msupply.com.ideaurben.Adapter.ReportcateorieslistAdapter$MyViewHolder$2.onResponse(ReportcateorieslistAdapter.java:384)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.xmlbeans.XmlOptions" on path: DexPathList[[zip file "/data/app/app.msupply.com.ideaurben-1/base.apk"],nativeLibraryDirectories=[/data/app/app.msupply.com.ideaurben-1/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at org.apache.poi.POIXMLDocumentPart.<clinit>(POIXMLDocumentPart.java:53) 
at app.msupply.com.ideaurben.Adapter.ReportcateorieslistAdapter$MyViewHolder$2.onResponse(ReportcateorieslistAdapter.java:384) 
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) 
at android.os.Handler.handleCallback(Handler.java:746) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
Suppressed: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 11 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available   

我的 libs 文件夹:我在libs文件夹中添加了所有库。 enter image description here

我的java类代码:

    try {

            String FILE_NAME = "/tmp/MyFirstExcel.xlsx";
            XSSFWorkbook workbook = new XSSFWorkbook();

          XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
          Object[][] datatypes = {
                  {"Datatype", "Type", "Size(in bytes)"},
                  {"int", "Primitive", 2},
                  {"float", "Primitive", 4},
                  {"double", "Primitive", 8},
                  {"char", "Primitive", 1},
                  {"String", "Non-Primitive", "No fixed size"}
          };
          int rowNum = 0;
          System.out.println("Creating excel");
          Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = sheet.iterator();
          for (Object[] datatype : datatypes) {
              Row row = sheet.createRow(rowNum++);
              int colNum = 0;
              for (Object field : datatype) {
                  Cell cell = row.createCell(colNum++);
                  if (field instanceof String) {
                      cell.setCellValue((String) field);
                  } else if (field instanceof Integer) {
                      cell.setCellValue((Integer) field);
                  }
              }
          }
          FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
          workbook.write(outputStream);
        //  workbook.close();


      } catch (IOException e) {
          e.printStackTrace();
      } /*catch (JSONException e) {
          e.printStackTrace();
      }*//*catch (FileNotFoundException e) {
          e.printStackTrace();
      }*/

  }

Build.gradel 文件如下。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "app.msupply.com.ideaurben"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        dexOptions {
            jumboMode true
        }
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
        }

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.+'
    compile 'com.android.support:support-v4:26.+'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
    compile 'com.android.support:cardview-v7:25.2.0'
    testCompile 'junit:junit:4.12'
}

请帮助任何人,提前致谢。

1 个答案:

答案 0 :(得分:2)

我想在NoClassDefFoundError上纠正其他人的观点。

NoClassDefFoundError可能由于多种原因而发生,例如

  1. ClassNotFoundException - 找不到该引用类的.class,无论它是否在编译时可用(即base / child class)。
  2. 找到类文件,但在初始化静态变量时引发了异常
  3. 找到类文件,初始化静态块时引发异常
  4. 说“编译时可用”是什么意思?

    • 引用的类在代码中使用。 例如:两个类,A和B(扩展A)。如果在代码中直接引用B,则它在编译时可用,即A a = new B();

    说“编译时不可用”是什么意思?

    • 编译时类和运行时类是不同的,例如,使用子类的类名加载基类,例如Class.forName(“classname”) 例如:两个类,A和B(扩展A)。代码已

      a a = Class.forName(“B”)。newInstance();