2015-07-10 181 views
0

我成功在仿真器中运行我的第一个uiautomator测试用例,并且一切正常。但是当我在同一个模拟器中第二次启动相同的测试案例时。我得到这个错误:NullPointer在仿真器上运行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

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

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

可能becasue 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; 
    } 
} 
+0

HEJ,感谢您的帮助。我也在这里检查,但问题是这个函数如何返回null? – gxlzlihao

+0

为什么使用DisplayManagerGlobal而不是UiDevice? –

相关问题