gpt4 book ai didi

java - 在 Android 中运行单元测试时, Intent 解析为不同的进程

转载 作者:太空狗 更新时间:2023-10-29 13:40:27 36 4
gpt4 key购买 nike

我有一个使用两个 Activity 的小型应用程序。这两个 Activity 都继承自 MapActivity 并显示 map (com.google.android.maps)。

因为 Android 谷歌地图文档说

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

我修改了我的 list 以在两个不同的进程中运行这两个 Activity (我删除了一些行以使其简短):

<?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>

现在应用程序运行良好,但是当我在两个 Activity 上运行单元测试时遇到问题。例如:

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() 方法时抛出异常:

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)

有没有办法让单元测试“解析”正确的流程?

最佳答案

Instrumentation 在同一进程中运行所有应用程序组件。

enter image description here

关于java - 在 Android 中运行单元测试时, Intent 解析为不同的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6445247/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com