标题是,我最近在运行我的android测试时遇到NoClassDefFoundError异常。该类是一个自定义类。我知道这可能是类路径的问题,但我不知道在Ubuntu上设置类路径的位置。
[UPDATE]
这是在调用getIdlingResource()
时调用错误的类:
public final class ToastManager {
private static final CountingIdlingResource idlingResource = new CountingIdlingResource("toast");
private static final View.OnAttachStateChangeListener listener = new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(final View v) {
idlingResource.increment();
}
@Override
public void onViewDetachedFromWindow(final View v) {
idlingResource.decrement();
}
};
private ToastManager() { }
public static Toast makeText(final Context context, final CharSequence text, final int duration) {
Toast t = Toast.makeText(context, text, duration);
t.getView().addOnAttachStateChangeListener(listener);
return t;
}
// For testing
public static IdlingResource getIdlingResource() {
return idlingResource;
}
}
这是错误日志:
java.lang.NoClassDefFoundError: com/idreamsglobal/idreams/utils/ToastUtil
at com.idreamsglobal.idreams.RegisterActivityTest.tearDown(RegisterActivityTest.java:168)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)
答案 0 :(得分:0)
我解决了Jerome和Jarod的答案相结合的问题:
sudo apt-get install openjdk-7-jdk
sudo nano /etc/environment
添加以下行:JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-i386
另外我刚发现https://stackoverflow.com/a/17827697/2533809似乎有一个很好的写作,几乎相同的答案。
(我正在使用Debian 7 Wheezy)
答案 1 :(得分:0)
在'项目'查看,右键点击应用程序'模块并选择“打开模块设置”。然后点击“依赖关系”'选项卡在顶部。下面的框将列出类路径。默认情况下,它还包含* .jar。
如果你正在使用的ToastUtil在jar中,我想它应该默认添加。但是,请尝试在依赖项下手动添加该jar。 如果是您编写的文件,请检查是否已正确导入。
此外,只需尝试使用终端将文件移出项目并构建错误代码。然后在适当的包中再次添加它并尝试干净的构建。有时候,那就行了。
希望有所帮助!