我一直在测试一些我没有源代码的软件包,其中一个软件包通常是通过按三个按钮三秒钟启动的。当我尝试使用典型的方法启动软件包时,我收到了一个java.lang.NullPointerException:尝试调用虚拟方法'android.content.Intent android.content.Intent.addFlags(int)
错误。以下是我的代码。
@Before
public void setup() {
//Initialize UiDevice instance
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
mDevice = UiDevice.getInstance(instrumentation);
mDevice.pressHome();
final String launcherPackage = mDevice.getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
Context context = InstrumentationRegistry.getTargetContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(DEALER_DIAG_PACKAGE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
mDevice.wait(Until.hasObject(By.pkg(DEALER_DIAG_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
我尝试使用getContext
而不是getTargetContext
,但有人指出,如果意图未公开,则无论如何我都无法以这种方式启动包。我尝试使用命令adb logcat ActivityManager:V *:F
和adb shell pm list packages -f
来获取软件包名称。
--------- beginning of main
I/ActivityManager( 2296): START u0 {flg=0x10000000
cmp=com.android.systemui/.usb.UsbDebuggingActivity (has extras)} from uid
1000 on display 0
I/ActivityManager( 2296): Displayed
com.android.systemui/.usb.UsbDebuggingActivity: +184ms
I/ActivityManager( 2296): START u0 {act=android.intent.action.MAIN cat=
[android.intent.category.HOME] flg=0x10200000
cmp=com.android.launcher3/.Launcher} from uid 1000 on display 0
I/ActivityManager( 2296): START u0
{act=com.REDACTED.auto.diagnostics.dealer.MAIN flg=0x10800000
cmp=com.REDACTED.auto.diagnostics/.dealer.MainActivity} from uid 1000 on
display 0
I/ActivityManager( 2296): Start proc
20943:com.REDACTED.auto.diagnostics/1000 for activity
com.REDACTED.auto.diagnostics/.dealer.MainActivity
I/ActivityManager( 2296): Displayed
com.REDACTED.auto.diagnostics/.dealer.MainActivity: +572ms
有人知道为什么我会收到这个错误吗?我已经尝试使用日志记录中列出的每个包名,但都没有成功。欢迎任何建议。