为什么要得到这个堆栈跟踪?

Why am getting this stacktrace?

编译我的代码后,我得到了这个异常

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.

我正在使用带有 ADT 插件的 eclipse IDE。

import android.support.test.uiautomator.*;
import android.support.test.InstrumentationRegistry;
import android.support.test.internal.runner.junit3.*;
import android.support.test.internal.runner.listener.InstrumentationResultPrinter;
import android.support.test.rule.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import android.os.RemoteException;
import android.support.*;

@RunWith(JUnit4.class)
public class uia {
    private UiDevice d;

    @Before
    public void setup() throws RemoteException {
        d = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        d.isScreenOn();
    }

    @Test
    public void home(){
        d.pressHome();
    }
}

在 运行 之后,此代码得到以下跟踪:

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
    at android.support.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
    at ui.uia.setup(uia.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod.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.RunBefores.evaluate(RunBefores.java:24)
    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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

有什么建议吗?

仪器测试需要 @RunWith(AndroidJUnit4.class) 而不是 @RunWith(JUnit4.class)

请查看documentation了解更多详情,