3

When I init UiDevice I got a NullPointerExecption , here is my error information:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.UiAutomation.setOnAccessibilityEventListener(android.app.UiAutomation$OnAccessibilityEventListener)' on a null object reference here is my code

Code:

@RunWith(AndroidJUnit4.class)
public class DailyTest  { 

    @Rule
        public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule(MainActivity.class, false, true);
    @Before
     public  void  setUp(){
            Instrumentation instrumentation =InstrumentationRegistry.getInstrumentation();
            Log.d(TAG,"instrumentation="+instrumentation);
            UiDevice uiDevice = UiDevice.getInstance(instrumentation);
            Log.d(TAG,"uiDevice="+uiDevice);
    }
}
tianyu
  • 119
  • 1
  • 10
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Manohar Apr 10 '19 at 09:08
  • @ManoharReddy no this is not a duplicate question. What you have shared here is a generic null pointer exception and tianyu is getting this error specifically in Android instrumented tests. I suspect the wrong annotation is causing that issue. – Sushant Somani Apr 10 '19 at 09:12
  • @Sushant Somani ,it is Test and Before made this problem? – tianyu Apr 10 '19 at 10:48

2 Answers2

0

You can initialize UiDevice object only in the test method. Currently you have annotated method with @Before which can't initialize UiDevice.

Its better to change the annotation to @Test or initialize UiDevice in method annotated with @Test

If this doesn't help, please share you Module level build.gradle.

Sushant Somani
  • 1,450
  • 3
  • 13
  • 31
  • hello,i initialize UiDevice object in @Test ,but it still error, – tianyu Apr 10 '19 at 10:07
  • this is my build.gradle:dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' implementation 'com.android.support.test:runner:1.0.2' implementation 'com.android.support.test:rules:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3' } – tianyu Apr 10 '19 at 10:07
  • defaultConfig { applicationId "com.xiangtianyu.dailytest" minSdkVersion 26 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } – tianyu Apr 10 '19 at 10:07
  • i solve this problem ,i should use adb command:adb shell am instrument -w -r -e class xxx ,but i use adb shell am instrument -e class xxx , – tianyu Apr 10 '19 at 12:10
0

try running the tests on adb with -w flag. This may help.

adb shell am instrument -w com.example.app.test/android.support.test.runner.AndroidJUnitRunner
Venkataramanan
  • 149
  • 1
  • 15