无法解析以下活动:仪器测试 Android 活动时的意图
当我尝试在 Android 上运行仪器测试时出现错误。我已经编写了一个名为 AudioPlayerActivity 的 Activity,它位于 com.mycompany.mobile.android.gui 包中,现在我正在尝试测试它的 GUI项目,我运行时出现以下错误:
java.lang.RuntimeException:无法解析以下活动:Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.mycompany.mobile.android.gui/.AudioPlayerActivity }
我已阅读 这个问题,以及遵循了那里的建议,但没有效果。我也用谷歌搜索了该错误,但没有找到任何帮助。
这是我的测试项目的 AndroidManifest.xml 的样子:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.mobile.android.gui"
android:versionCode="1"
android:versionName="1.0" >
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.mycompany.mobile.android.gui" />
<application>
<uses-library android:name="android.test.runner" />
</application>
<uses-sdk android:targetSdkVersion="7" />
<supports-screens android:anyDensity="true" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
这是我的仪器测试。
包 com.mycompany.mobile.android.gui;
import android.test.ActivityInstrumentationTestCase2;
import com.mycompany.mobile.android.gui.AudioPlayerActivity;
public class TestMusicPlayerActivityTest extends ActivityInstrumentationTestCase2<AudioPlayerActivity> {
public TestMusicPlayerActivityTest() {
super("com.mycompany.mobile.android.gui", AudioPlayerActivity.class);
}
public TestMusicPlayerActivityTest(String name) {
super("com.mycompany.mobile.android.gui", AudioPlayerActivity.class);
}
public TestMusicPlayerActivityTest(String pkg, Class<AudioPlayerActivity> activityClass) {
super("com.mycompany.mobile.android.gui", AudioPlayerActivity.class);
}
public void testSomething() {
System.out.println("Nothing works :(");
System.out.println(getActivity());
}
}
我们通过 Maven 运行整个过程,但这不应该是问题的一部分。
正如您所看到的,测试的 Activity 也位于同一个包中。我不会像大多数遇到此问题的人那样使用活动名称而不是包名称来调用超级构造函数。
为了您的兴趣,这是描述测试活动的 AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.mobile.android.gui"
android:versionCode="1"
android:versionName="1.0" >
<!-- omitted Uses permission + Uses SDK -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true"
android:theme="@style/NoTitleBar"
>
<!-- omitted other activities -->
<activity
android:name="com.mycompany.mobile.android.gui.AudioPlayerActivity"
android:screenOrientation="portrait" ></activity>
</application>
</manifest>
我还将该活动添加到 AndroidManifest.xml,但这没有改变任何内容。我还尝试为所需的活动添加 MAIN/LAUNCHER 意图过滤器,但这并没有改变测试的结果。我尝试过在没有额外人员的情况下开始活动,这也没有改变结果。
I'm getting errors when I'm trying to run insturmentation tests on android. I've written an Activity, called AudioPlayerActivity that lies in the package com.mycompany.mobile.android.gui, and now I'm trying to test the GUI of that project and I'm running in the following error:
java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.mycompany.mobile.android.gui/.AudioPlayerActivity }
I have read this question, and followed the advice there, but to no avail. I also googled for the error, but did not find any help.
This is how my AndroidManifest.xml for the test project looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.mobile.android.gui"
android:versionCode="1"
android:versionName="1.0" >
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.mycompany.mobile.android.gui" />
<application>
<uses-library android:name="android.test.runner" />
</application>
<uses-sdk android:targetSdkVersion="7" />
<supports-screens android:anyDensity="true" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
And here's my Instrumentation test.
package com.mycompany.mobile.android.gui;
import android.test.ActivityInstrumentationTestCase2;
import com.mycompany.mobile.android.gui.AudioPlayerActivity;
public class TestMusicPlayerActivityTest extends ActivityInstrumentationTestCase2<AudioPlayerActivity> {
public TestMusicPlayerActivityTest() {
super("com.mycompany.mobile.android.gui", AudioPlayerActivity.class);
}
public TestMusicPlayerActivityTest(String name) {
super("com.mycompany.mobile.android.gui", AudioPlayerActivity.class);
}
public TestMusicPlayerActivityTest(String pkg, Class<AudioPlayerActivity> activityClass) {
super("com.mycompany.mobile.android.gui", AudioPlayerActivity.class);
}
public void testSomething() {
System.out.println("Nothing works :(");
System.out.println(getActivity());
}
}
We are running the entire process via maven, but that should not be a part of the problem.
The tested Activity, as you can see, also lies in the same package. I don't call the super constructor with the activity's instead of the package name, as most people having this issue do.
For your interest, this is the AndroidManifest.xml describing the tested activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.mobile.android.gui"
android:versionCode="1"
android:versionName="1.0" >
<!-- omitted Uses permission + Uses SDK -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true"
android:theme="@style/NoTitleBar"
>
<!-- omitted other activities -->
<activity
android:name="com.mycompany.mobile.android.gui.AudioPlayerActivity"
android:screenOrientation="portrait" ></activity>
</application>
</manifest>
I also added the activity to the AndroidManifest.xml, but this didn't change anything. I also tried adding the MAIN/LAUNCHER intent filter for the desired activity, but to this didn't change the outcome of the test. I have tried starting the activity with extras and without them, that also didn't change the outcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 Android 项目和 Android 测试项目是否使用相同的包名称?这可能是一个问题,但我不确定它是否会导致您的 java.lang.RuntimeException。
根据官方开发指南此处,您的测试项目必须具有与您测试的项目不同的包名称:
尝试为您的测试项目使用包名称 com.mycompany.mobile.android.gui.test。
Are you using the same package name for both your Android project and Android test project? This could be a problem, though I am not sure if it causes your java.lang.RuntimeException.
According to the official dev guide here, Your test project must have a different package name from you tested project:
Try using package name com.mycompany.mobile.android.gui.test for your test project.
有两种可能的解决方案。
如果您忘记在 Android 清单中定义活动,您将收到“无法解决活动错误”。如果您要在 Android 测试项目中创建单独的测试活动,请创建一个单独的测试子包并在其中创建您的活动。之后,将其包含在您的 Android 清单中并将其标记为导出。
默认情况下,android Gradle 插件会将
.test
附加到测试应用程序的applicationId
和namespace
中。因此您需要将所有类放在单独的测试包中。namespace
和applicationId
。您可以在模块级
build.gradle
文件中定义测试应用程序的namespace
和applicationId
。将其设置为与您的测试应用程序的包名称匹配。建议对
namespace
和applicationId
使用相同的值。如果您使用不同的值,Android Gradle 插件将复制applicationId
代替 Android 清单中的namespace
。There are two possible solutions.
If you forgot to define activity in your android manifest, you would get "Unable to resolve activity error". If you are creating separate test activity in your android test project, create a separate test sub package and create your activity there. After that include it in your android manifest and mark it as exported.
By default the android Gradle plugin appends
.test
to theapplicationId
andnamespace
of the test app. So that you need to put all your classes in a separate test package.namespace
and theapplicationId
.You can define
namespace
andapplicationId
of the test app in module levelbuild.gradle
file. Set it to match your test app's package name.It is recommended to use same values for the
namespace
and theapplicationId
. If you use different values, the android Gradle plugin will copyapplicationId
in place ofnamespace
in the android manifest.您正在将其作为 Android 项目运行,请尝试将其作为 Android 测试项目运行
adb shell am Instrument -e package; -w <测试apk包>/android.test.InstrumentationTestRunner
You are running it as an Android Project, try running it as a Android Test Project
adb shell am instrument -e package <java package> -w <test apk package>/android.test.InstrumentationTestRunner