1

我一直在测试一些我没有源代码的包,其中一个包通常通过按下三个按钮三秒钟来启动。当我尝试使用典型方法启动包时,出现java.lang.NullPointerException: Attempt to invoke virtual method '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

有人对我为什么会收到此错误有任何意见吗?我尝试使用 logcat 转储中列出的每个包名称都没有成功。任何输入将不胜感激。

4

2 回答 2

1

如果您想启动另一个应用程序,只需首先检查该应用程序是否已安装,否则您将获得一个NullPointerException,然后通过带有包选项的意图启动该应用程序:

像这样:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.domain.anotherapp");
        if (launchIntent != null) {
            startActivity(launchIntent);//null pointer check in case package name was not found
        }

这将使用默认启动活动启动应用程序,如果您想启动特定活动,您必须知道如何处理另一应用程序端的要求,否则会崩溃或无法工作(可能您可能需要传递一些变量或一些数据来表示这个应用程序的先前信息),无论如何,要打开这个特定的活动,你必须使用ComponentName.

ComponentName带两个s的构造函数String可用于引用另一个应用程序中的组件。但是,第一个参数不是类的包名;它是应用程序的包名——该应用程序中元素的package属性。所以你的第一个例子应该是manifestAndroidManifest.xml

ComponentName cn = new ComponentName("com.domain.anotherapp",
    "com.domain.anotherapp.widget.WidgetProvider");

该构造函数当然可以用于引用您自己的应用程序中的组件,但是由于您已经Context从自己的应用程序中获取了一个,您不妨使用它并使用其他构造函数之一。在我看来,Class只要可用,就应该首选采用 a 的那个。String如果您出于某种原因仅动态了解该类,则可以使用采取 a的方法;在这种情况下,它应该采用上述完全限定的类名。

完全使用意图启动(不处理错误传递信息的空指针异常):

Intent launchIntent = new Intent(Intent.ACTION_MAIN);
                launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                ComponentName cn = new ComponentName("com.domain.anotherapp",
                "com.domain.anotherapp.widget.WidgetProvider");
                launchIntent.setComponent(cn);

                startActivity(launchIntent);

参考: https ://developer.android.com/reference/android/content/ComponentName.html

于 2017-06-06T15:57:34.703 回答
0

我想出了一种对我有用的方法,尽管 Brandon 可能会采用另一种解决方案。这是我的解决方案:

Intent intent = new Intent("com.REDACTED.auto.diagnostics.dealer.MAIN");
intent.setClassName("com.REDACTED.auto.diagnostics", 
"com.REDACTED.auto.diagnostics.dealer.MainActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Context c = InstrumentationRegistry.getContext();
c.startActivity(intent);

这可能与 Brandon 的解决方案所做的相同,但不那么抽象。

于 2017-06-06T18:13:03.313 回答