gpt4 book ai didi

android - 使用 startInstrumentation() 启动 Instrumentation 时 UiDevice.getInstance(getInstrumentation()) 崩溃(作为空对象引用)

转载 作者:行者123 更新时间:2023-11-29 01:30:30 27 4
gpt4 key购买 nike

Android SDK:22(长)UiAutomator 版本:'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

在单击按钮时,我正在尝试使用

从 Android 应用程序访问 Instrumentation
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 instrument -w com.automation.vzw.sanity.test/android.test.InstrumentationTestRunner)

请帮助克服这个问题

谢谢

最佳答案

这不是您获得仪器的方式。使用

Instrumentation instr = InstrumentationRegistry.getInstrumentation();

访问仪器。

然后就可以获得一个UiDevice对象:

UiDevice device = UiDevice.getInstance(instr);

关于android - 使用 startInstrumentation() 启动 Instrumentation 时 UiDevice.getInstance(getInstrumentation()) 崩溃(作为空对象引用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31474776/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com