UiDevice.getInstance(getInstrumentation()) Сбой (как ссылка на нулевой объект) при запуске инструментария с помощью startInstrumentation()

Android SDK: 22 (L) Версия UiAutomator: 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

При нажатии кнопки я пытаюсь получить доступ к инструментам из приложения Android, используя

Boolean start = false;
start = startInstrumentation(new ComponentName("com.automation.vzw.sanity.test", "android.test.InstrumentationTestRunner"), null, null);
  System.out.println("value of start is " +start);

Значение start показывает «true», что указывает на то, что он был запущен.

Проблема в том, что после запуска Instrumentation и вызова setUp(), когда выполняется приведенное ниже, происходит сбой

public class ApplicationTest extends InstrumentationTestCase {

 public void setUp() {
              UiDevice testDevice = UiDevice.getInstance(getInstrumentation());
             }

 public void testCase1(){

        System.out.println("In testcase1");

}

}

Детали аварии:

/TestRunner(11209): started: testCase1(com.automation.vzw.sanity.ApplicationTest)

I/TestRunner(11209): failed: testCase1(com.automation.vzw.sanity.ApplicationTest)

I/TestRunner(11209): ----- begin exception -----

I/TestRunner(11209): 

I/TestRunner(11209):   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.UiAutomation.setOnAccessibilityEventListener(android.app.UiAutomation$OnAccessibilityEventListener)' on a null object reference

I/TestRunner(11209):    at android.support.test.uiautomator.UiAutomatorBridge.setOnAccessibilityEventListener(UiAutomatorBridge.java:78)

I/TestRunner(11209):    at android.support.test.uiautomator.QueryController.<init>(QueryController.java:58)

I/TestRunner(11209):    at android.support.test.uiautomator.UiAutomatorBridge.<init>(UiAutomatorBridge.java:66)

I/TestRunner(11209):    at android.support.test.uiautomator.InstrumentationUiAutomatorBridge.<init>(InstrumentationUiAutomatorBridge.java:35)

I/TestRunner(11209):    at android.support.test.uiautomator.UiDevice.<init>(UiDevice.java:103)

I/TestRunner(11209):    at android.support.test.uiautomator.UiDevice.getInstance(UiDevice.java:263)

I/TestRunner(11209):    at com.automation.vzw.sanity.ApplicationTest.setUp(ApplicationTest.java:103)

I/TestRunner(11209):    at junit.framework.TestCase.runBare(TestCase.java:132)

I/TestRunner(11209):    at junit.framework.TestResult$1.protect(TestResult.java:115)

I/TestRunner(11209):    at junit.framework.TestResult.runProtected(TestResult.java:133)

I/TestRunner(11209):    at junit.framework.TestResult.run(TestResult.java:118)

I/TestRunner(11209):    at junit.framework.TestCase.run(TestCase.java:124)

I/TestRunner(11209):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)

I/TestRunner(11209):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)

I/TestRunner(11209):    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)

I/TestRunner(11209):    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1944)

I/TestRunner(11209): ----- end exception -----

I/TestRunner(11209): finished: testCase1(com.automation.vzw.sanity.ApplicationTest)

I/ActivityManager(30686): Force stopping com.automation.vzw.sanity appid=10175 user=0: finished inst

I/ActivityManager(30686): Killing 11209:com.automation.vzw.sanity/u0a175 (adj 0): stop com.automation.vzw.sanity

I/ServiceManager(31912): Waiting for service SurfaceFlinger...

W/ActivityManager(30686): Spurious death for ProcessRecord{2a8ec647 11209:com.automation.vzw.sanity/u0a175}, curProc for 11209: null

Примечание: при выполнении из командной строки проблем нет, работает хорошо (am tools -w com.automation.vzw.sanity.test/android.test.InstrumentationTestRunner)

Пожалуйста, помогите преодолеть это

Спасибо


person Gautham    schedule 17.07.2015    source источник


Ответы (1)


Это не то, как вы получаете инструменты. Использовать

Instrumentation instr = InstrumentationRegistry.getInstrumentation();

для доступа к приборам.

Затем вы можете получить объект UiDevice:

UiDevice device = UiDevice.getInstance(instr);
person Inês    schedule 25.11.2015
comment
который из? Первая или вторая команда? У вас активирован режим разработчика на телефоне? - person Inês; 16.09.2016
comment
второй. да, режим разработчика включен - person likejudo; 17.09.2016
comment
Убедитесь, что вы: 1) реализуете свой код в папке тестового проекта (androidTest); 2) реализуя его как тесты junit 4 3) вы получаете доступ к реестру инструментов и т. д. в методе setUp() - person Inês; 22.09.2016
comment
Я устал от описанных выше шагов, но все еще получаю нулевой указатель. Удалось ли вам решить эту проблему? - person Ajit Pratap Singh; 29.09.2016