无法解析以下活动:仪器测试 Android 活动时的意图

发布于 2024-12-27 01:30:43 字数 3266 浏览 0 评论 0原文

当我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失眠症患者 2025-01-03 01:30:43
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.mycompany.mobile.android.gui"
... ...

您的 Android 项目和 Android 测试项目是否使用相同的包名称?这可能是一个问题,但我不确定它是否会导致您的 java.lang.RuntimeException。

根据官方开发指南此处,您的测试项目必须具有与您测试的项目不同的包名称:

Android 包名称是 .apk 文件的唯一系统名称,由包清单中元素的“android:package”属性设置。 测试包的 Android 包名称必须与被测应用程序的 Android 包名称不同。默认情况下,Android 工具通过在包名称后附加“.test”来创建测试包名称。正在测试的应用程序。

尝试为您的测试项目使用包名称 com.mycompany.mobile.android.gui.test。

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.mycompany.mobile.android.gui"
... ...

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:

An Android package name is a unique system name for a .apk file, set by the "android:package" attribute of the element in the package's manifest. The Android package name of your test package must be different from the Android package name of the application under test. By default, Android tools create the test package name by appending ".test" to the package name of the application under test.

Try using package name com.mycompany.mobile.android.gui.test for your test project.

二智少女 2025-01-03 01:30:43

有两种可能的解决方案。

  1. 在 android 清单中添加活动定义并将其标记为导出。

如果您忘记在 Android 清单中定义活动,您将收到“无法解决活动错误”。如果您要在 Android 测试项目中创建单独的测试活动,请创建一个单独的测试子包并在其中创建您的活动。之后,将其包含在您的 Android 清单中并将其标记为导出。

# folder structure
└── src
    └── androidTest
        └── java
            └── com
                └── example
                    └── test
                        └── TestActivity.kt

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.test">
  <application>
     <activity
       android:name=".TestActivity"
       android:exported="true" />
  </application>
</manifest>

默认情况下,android Gradle 插件会将 .test 附加到测试应用程序的 applicationIdnamespace 中。因此您需要将所有类放在单独的测试包中。

  1. 更改 android 测试 namespaceapplicationId

您可以在模块级 build.gradle 文件中定义测试应用程序的 namespaceapplicationId。将其设置为与您的测试应用程序的包名称匹配。

android {
  testNamespace 'com.example.app'
  defaultConfig {
     testApplicationId 'com.example.app'
  }
}

建议对 namespaceapplicationId 使用相同的值。如果您使用不同的值,Android Gradle 插件将复制 applicationId 代替 Android 清单中的 namespace

There are two possible solutions.

  1. Add activity definition in the android manifest and mark it as exported.

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.

# folder structure
└── src
    └── androidTest
        └── java
            └── com
                └── example
                    └── test
                        └── TestActivity.kt

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.test">
  <application>
     <activity
       android:name=".TestActivity"
       android:exported="true" />
  </application>
</manifest>

By default the android Gradle plugin appends .test to the applicationId and namespace of the test app. So that you need to put all your classes in a separate test package.

  1. Change android test namespace and the applicationId.

You can define namespace and applicationId of the test app in module level build.gradle file. Set it to match your test app's package name.

android {
  testNamespace 'com.example.app'
  defaultConfig {
     testApplicationId 'com.example.app'
  }
}

It is recommended to use same values for the namespace and the applicationId. If you use different values, the android Gradle plugin will copy applicationId in place of namespace in the android manifest.

最初的梦 2025-01-03 01:30:43

您正在将其作为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文