NullPointer在仿真器上运行uiautomator测试用例时出错

时间:2015-07-10 09:32:57

标签: android uiautomator android-uiautomator

我在模拟器中成功运行了我的第一个uiautomator测试用例,一切都很顺利。但是当我在同一个模拟器中第二次启动相同的测试用例时。我收到了这个错误:

INSTRUMENTATION_STATUS: stack=java.lang.NullPointerException
at com.android.uiautomator.core.ShellUiAutomatorBridge.getDefaultDisplay(ShellUiAutomatorBridge.java:50)
at com.android.uiautomator.core.UiDevice.getDisplayWidth(UiDevice.java:378)
at com.android.uiautomator.core.UiDevice.click(UiDevice.java:408)
at test.uiautomator.ApiTest.testCase(ApiTest.java:172)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAutomatorTestRunner.java:160)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutomatorTestRunner.java:96)
at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.java:91)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
at dalvik.system.NativeStart.main(Native Method)

有人知道原因吗?我没有修改测试用例中的任何一行,我只是第二次运行它。

1 个答案:

答案 0 :(得分:0)

我查了代码,这里发生错误:

public Display getDefaultDisplay() {
    return  DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);// Nullpointer here. 
}

可能因为DisplayManagerGlobal.getInstance()返回NULL。

见代码:

/**
 * Gets an instance of the display manager global singleton.
 *
 * @return The display manager instance, may be null early in system startup
 * before the display manager has been fully initialized.
 */
public static DisplayManagerGlobal getInstance() {
    synchronized (DisplayManagerGlobal.class) {
        if (sInstance == null) {
            IBinder b = ServiceManager.getService(Context.DISPLAY_SERVICE);
            if (b != null) {
                sInstance = new DisplayManagerGlobal(IDisplayManager.Stub.asInterface(b));
            }
        }
        return sInstance;
    }
}