Intent resolved to different process when running Unit Test in Android

匿名 (未验证) 提交于 2019-12-03 02:27:02

问题:

I have a small application that uses two activities. Both the activities inherit from MapActivity and display a map (com.google.android.maps).

Since the Android Google Map documentation says

Only one MapActivity is supported per process. Multiple MapActivities running simultaneously are likely to interfere in unexpected and undesired ways.

I modified my manifest to run the two activities in two different processes (I have removed some lines to make it short):

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp">  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />  <application     android:icon="@drawable/icon"     android:label="@string/app_name"     android:theme="@android:style/Theme.Light">      <uses-library android:name="com.google.android.maps" />      <activity          android:name=".Activity1"         android:process=".Activity1">         <intent-filter>             <action android:name="android.intent.action.MAIN" />             <category android:name="android.intent.category.LAUNCHER" />         </intent-filter>Unit     </activity>      <activity         android:name=".Activity2"         android:process=".Activity2">         <intent-filter>             <action android:name="android.intent.action.MAIN" />             <category android:name="android.intent.category.DEFAULT" />         </intent-filter>     </activity>  </application>  <uses-sdk android:minSdkVersion="8" /> </manifest>  

Now the application runs fine but I have problems when I what to run Unit Tests on both the Activities. For example:

package com.example.myapp; public class Activity1Test extends ActivityInstrumentationTestCase2<Activity1> {      Activity1 mActivity;      public Activity1Test() {         super("com.example.myapp.Activity1", Activity1.class);     }      @Override      protected void setUp() throws Exception {         super.setUp();         setActivityInitialTouchMode(false);         setActivityIntent(new Intent());         mActivity = getActivity();  //An exception is thrown at this line     } } 

When I call the getActivity() method an exception is thrown:

java.lang.RuntimeException: Intent in process com.example.myapp resolved to different process .Activity1: Intent { flg=0x10000000 cmp=com.example.myapp/.Activity1 } at android.app.Instrumentation.startActivitySync(Instrumentation.java:377) at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119) at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:100) at com.example.myapp.Activity1Test.setUp(Activity1Test.java:28) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447) 

Is there a way to make the Unit Test to "resolve" the correct process?

回答1:

Instrumentation runs all of your application components in the same process.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!