在Android上使用objenesis时出现NoClassDefFoundError

时间:2012-09-08 18:24:51

标签: android noclassdeffounderror cloning

我正在开发一个使用com.rits.cloningorg.objenesis.*库来深度克隆对象的Android应用程序。 目的是将相同的对象添加到我的树结构类两次或更多,而不必担心原始对象引用同一个对象。 由于常规clone()只是浅层复制对象,我使用上面提到的库。

在开发android项目之前,我创建了一个java项目来实现我的树,并且使用这些库工作得很好。 然后我将它导入android项目(并添加com.rits.cloningorg.objenesis.*作为外部lib,然后检查Java Build Path > Order and Export中的两个库。 但是当我运行它时,就在我调用deepClone()的行上,此错误出现在LogCat中并且应用程序已强行关闭:

E/AndroidRuntime(280): FATAL EXCEPTION: main
E/AndroidRuntime(280): java.lang.NoClassDefFoundError: sun.reflect.ReflectionFactory
E/AndroidRuntime(280):  at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.<init>(SunReflectionFactoryInstantiator.java:40)
E/AndroidRuntime(280):  at org.objenesis.strategy.StdInstantiatorStrategy.newInstantiatorOf(StdInstantiatorStrategy.java:85)
E/AndroidRuntime(280):  at org.objenesis.ObjenesisBase.getInstantiatorOf(ObjenesisBase.java:90)
E/AndroidRuntime(280):  at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
E/AndroidRuntime(280):  at com.rits.cloning.Cloner.newInstance(Cloner.java:291)
E/AndroidRuntime(280):  at com.rits.cloning.Cloner.cloneInternal(Cloner.java:468)
E/AndroidRuntime(280):  at com.rits.cloning.Cloner.deepClone(Cloner.java:327)
E/AndroidRuntime(280):  at com.susterblonde.project_monitoring.project.WBS.add(WBS.java:35)
E/AndroidRuntime(280):  at com.susterblonde.project_monitoring.project.ProjectUtility.demoPlan(ProjectUtility.java:101)
E/AndroidRuntime(280):  at com.susterblonde.project_monitoring.ProjectSelection.onCreate(ProjectSelection.java:45)
E/AndroidRuntime(280):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(280):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(280):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(280):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(280):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(280):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(280):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(280):  at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(280):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(280):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(280):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(280):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(280):  at dalvik.system.NativeStart.main(Native Method)

我用谷歌搜索并分别尝试了这个:

  • 清理项目
  • 从构建路径中删除com.rits.cloningorg.objenesis.*并阅读
  • 重新排序这些库,使它们位于Android Dependencies正下方
  • 重新排序这些库,使它们位于所有库之上

我认为这不是因为eclipse更新,因为我在发生这种情况前几周更新了它。

这是源代码:

package com.susterblonde.tree;

import java.util.ArrayList;
import com.rits.cloning.Cloner;

public class MyTree {
    Data o;
    MyTree parent;
    ArrayList<MyTree> child = new ArrayList<MyTree>();

    public void add(MyTree tree) {
        Cloner c = new Cloner();

        MyTree temp =   c.deepClone(tree); //this is where the app crashed
        temp.parent = this;
        child.add(temp);
    }

    public static void main(String[] args) {
        MyTree tree1 = new MyTree();
        MyTree tree2 = new MyTree();

        tree1.add(tree2);
        tree1.add(tree2);
        tree1.add(tree2);
        //The result wanted here is tree1 has 3 different but identical child objects
        //NOT 3 child which refer to the same one object
    }
}

class Data {
    double value;
}

问题:

  • 为什么我会遇到这个错误?如何摆脱它?
  • 有没有其他方法可以实现我的树?

原谅我的英语。

谢谢:)

1 个答案:

答案 0 :(得分:1)

在Eclipse中,当您在Java Build Path对话框中添加外部库时,不要忘记切换到Order and Export选项卡并在列表中勾选该库的名称。这是必需的,以便在运行时找到库,而不仅仅是在编译时。

NoClassDefFoundError when running Instrumentation test with ant

相关问题