android - UiDevice.getInstance(getInstrumentation()) Crashes (as Null object reference) when Launching the Instrumentation using startInstrumentation() -


android sdk : 22 (l) uiautomator version : 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

on button click trying access instrumentation android application using

boolean start = false; start = startinstrumentation(new componentname("com.automation.vzw.sanity.test", "android.test.instrumentationtestrunner"), null, null);   system.out.println("value of start " +start); 

value of start shows "true" indicates launched

problem : once instrumentation starts , setup() called , when below gets executed , there crash

public class applicationtest extends instrumentationtestcase {   public void setup() {               uidevice testdevice = uidevice.getinstance(getinstrumentation());              }   public void testcase1(){          system.out.println("in testcase1");  }  } 

crash details :

/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 invoke virtual method 'void android.app.uiautomation.setonaccessibilityeventlistener(android.app.uiautomation$onaccessibilityeventlistener)' on null object reference  i/testrunner(11209):    @ android.support.test.uiautomator.uiautomatorbridge.setonaccessibilityeventlistener(uiautomatorbridge.java:78)  i/testrunner(11209):    @ android.support.test.uiautomator.querycontroller.<init>(querycontroller.java:58)  i/testrunner(11209):    @ android.support.test.uiautomator.uiautomatorbridge.<init>(uiautomatorbridge.java:66)  i/testrunner(11209):    @ android.support.test.uiautomator.instrumentationuiautomatorbridge.<init>(instrumentationuiautomatorbridge.java:35)  i/testrunner(11209):    @ android.support.test.uiautomator.uidevice.<init>(uidevice.java:103)  i/testrunner(11209):    @ android.support.test.uiautomator.uidevice.getinstance(uidevice.java:263)  i/testrunner(11209):    @ com.automation.vzw.sanity.applicationtest.setup(applicationtest.java:103)  i/testrunner(11209):    @ junit.framework.testcase.runbare(testcase.java:132)  i/testrunner(11209):    @ junit.framework.testresult$1.protect(testresult.java:115)  i/testrunner(11209):    @ junit.framework.testresult.runprotected(testresult.java:133)  i/testrunner(11209):    @ junit.framework.testresult.run(testresult.java:118)  i/testrunner(11209):    @ junit.framework.testcase.run(testcase.java:124)  i/testrunner(11209):    @ android.test.androidtestrunner.runtest(androidtestrunner.java:191)  i/testrunner(11209):    @ android.test.androidtestrunner.runtest(androidtestrunner.java:176)  i/testrunner(11209):    @ android.test.instrumentationtestrunner.onstart(instrumentationtestrunner.java:555)  i/testrunner(11209):    @ 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 service surfaceflinger...  w/activitymanager(30686): spurious death processrecord{2a8ec647 11209:com.automation.vzw.sanity/u0a175}, curproc 11209: null 

note: when executed command line there no problem works (am instrument -w com.automation.vzw.sanity.test/android.test.instrumentationtestrunner)

please overcome this

thanks

that's not how instrumentation. use

instrumentation instr = instrumentationregistry.getinstrumentation(); 

to access instrumentation.

then can obtain uidevice object:

uidevice device = uidevice.getinstance(instr); 

Comments