在Android中运行单元测试时,Intent解析为不同的进程 [英] Intent resolved to different process when running Unit Test in Android
问题描述
我有一个使用两个活动的小应用程序。这两个活动都继承自MapActivity并显示地图(com.google.android.maps)。
I have a small application that uses two activities. Both the activities inherit from MapActivity and display a map (com.google.android.maps).
由于Android Google地图文档说
Since the Android Google Map documentation says
每个只支持一个MapActivity
流程。同时运行的多个MapActivities
很可能会以意外和不期望的
方式干预
。
Only one MapActivity is supported per process. Multiple MapActivities running simultaneously are likely to interfere in unexpected and undesired ways.
I修改我的清单以在两个不同的进程中运行这两个活动(我删除了一些行以使其变短):
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
}
}
当我调用 getActivity()
方法抛出异常:
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?
推荐答案
Instrumentation在同一个过程中运行所有应用程序组件。
Instrumentation runs all of your application components in the same process.
这篇关于在Android中运行单元测试时,Intent解析为不同的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!