Java UiAutomatorTestCase getUiDevice()NullPointerException

Java UiAutomatorTestCase getUiDevice()NullPointerException,java,android,adt,Java,Android,Adt,在UiAutomatorTestCase中调用getUiDevice()时,我得到null public class DemoTestCase extends UiAutomatorTestCase{ public void testDemo() { assertTrue(getUiDevice().pressHome()); Bundle status = new Bundle(); status.putString("msg", "Th

在UiAutomatorTestCase中调用getUiDevice()时,我得到null

public class DemoTestCase extends UiAutomatorTestCase{
    public void testDemo() {
        assertTrue(getUiDevice().pressHome());
        Bundle status = new Bundle();
        status.putString("msg", "This is a demo test and I just pressed HOME");
        status.putString("product", getUiDevice().getProductName());
        Point p = getUiDevice().getDisplaySizeDp();
        status.putInt("dp-width", p.x);
        status.putInt("dp-height", p.y);
        getAutomationSupport().sendStatus(Activity.RESULT_OK, status);
    }
}
我尝试过不同的例子,但总是得到null,我没有得到UI设备。此测试从应用程序的主Java类调用

我在运行uiautomator测试时使用android VirtualBox。我使用adb push发送应用程序,并使用adb shell uiautomator runtest运行它
my.jar-c my.main.Class


如果我不使用
getUiDevice()
一切运行正常。

如果从另一个类调用
getUiDevice()
,则必须在构造函数中声明变量,并从原始类调用它

public class OriginalClass extends UiAutomatorTestCase {
    public void runTestInAnotherClass() {
        DemoTestCase demoTestCase = new DemoTestCase(getUiDevice());
        testDemo();
    }

}

public class DemoTestCase extends UiAutomatorTestCase{
    UiDevice uiDevice;

    public DemoTestCase(UiDevice device) {
        uiDevice = device;
    }

    public void testDemo() {
        assertTrue(device.pressHome());
        Bundle status = new Bundle();
        status.putString("msg", "This is a demo test and I just pressed HOME");
        status.putString("product", device.getProductName());
        Point p = device.getDisplaySizeDp();
        status.putInt("dp-width", p.x);
        status.putInt("dp-height", p.y);
        getAutomationSupport().sendStatus(Activity.RESULT_OK, status);
    }
}