2017-06-06 67 views
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转储中列出的每个软件包名称都没有成功。任何输入将不胜感激。

回答

0

我想通了,为我工作的方法,尽管布兰登可能会在另一个解决方案上工作。这是我的解决方案:

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); 

这可能与布兰登的解决方案做同样的事情,但较少抽象。

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需要两个String s可用于引用另一个应用程序中的组件。但是,第一个参数不是该类的包名;它是应用程序的软件包名称 - 该应用程序的AndroidManifest.xmlmanifest元素的package属性。所以,你的第一个例子应该是

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

该构造函数,当然可以用来指代组件在自己的应用程序,但既然你已经有了一个Context的保持,从自己的应用程序,你还不如用它,并使用一个的其他构造函数。在我看来,只要可用,应该首选Class。如果因为某种原因只能动态地了解该类,则可以使用String;在这种情况下,它应该像上面那样采用完全合格的类名称。

完全使用由意向推出(不办理不正确的传递信息的空指针异常):

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(cp); 

       startActivity(launchIntent); 

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