简体   繁体   English

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

[英]Unable to resolve activity for: Intent when instrumentation-testing android activities

I'm getting errors when I'm trying to run insturmentation tests on android.当我尝试在 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:我编写了一个名为AudioPlayerActivity的活动,它位于 package com.mycompany.mobile.android.gui中,现在我正在运行该项目以尝试测试 GUI:

java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.mycompany.mobile.android.gui/.AudioPlayerActivity } java.lang.RuntimeException:无法解析活动: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:这就是我的测试项目的 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>

And here's my Instrumentation test.这是我的仪器测试。

package com.mycompany.mobile.android.gui; 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.我们正在通过 maven 运行整个过程,但这不应该是问题的一部分。

The tested Activity, as you can see, also lies in the same package.如您所见,测试的 Activity 也位于相同的 package 中。 I don't call the super constructor with the activity's instead of the package name, as most people having this issue do.我不会像大多数遇到此问题的人那样使用活动而不是 package 名称来调用超级构造函数。

For your interest, this is the AndroidManifest.xml describing the tested 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>

I also added the activity to the AndroidManifest.xml, but this didn't change anything.我还将活动添加到 AndroidManifest.xml,但这并没有改变任何东西。 I also tried adding the MAIN/LAUNCHER intent filter for the desired activity, but to this didn't change the outcome of the test.我还尝试为所需的活动添加 MAIN/LAUNCHER 意图过滤器,但这并没有改变测试的结果。 I have tried starting the activity with extras and without them, that also didn't change the outcome.我尝试过用额外的东西开始活动,没有它们,这也没有改变结果。

<?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? 您是否为Android项目和Android测试项目使用相同的软件包名称? This could be a problem, though I am not sure if it causes your java.lang.RuntimeException. 这可能是一个问题,但我不确定它是否会导致您的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. Android程序包名称是.apk文件的唯一系统名称,由程序包清单中元素的“android:package”属性设置。 The Android package name of your test package must be different from the Android package name of the application under test. 测试包的Android包名称必须与要测试的应用程序的Android包名称不同。 By default, Android tools create the test package name by appending ".test" to the package name of the application under test. 默认情况下,Android工具通过将“.test”附加到要测试的应用程序的包名称来创建测试包名称。

Try using package name com.mycompany.mobile.android.gui.test for your test project. 尝试将包名称com.mycompany.mobile.android.gui.test用于测试项目。

There are two possible solutions.有两种可能的解决方案。

  1. Add activity definition in the android manifest and mark it as exported.在 android 清单中添加活动定义并将其标记为已导出。

If you forgot to define activity in your android manifest, you would get "Unable to resolve activity error".如果您忘记在 android 清单中定义活动,您将收到“无法解决活动错误”。 If you are creating separate test activity in your android test project, create a separate test sub package and create your activity there.如果您在 android 测试项目中创建单独的测试活动,请创建一个单独的测试子 package 并在那里创建您的活动。 After that include it in your android manifest and mark it as exported.之后将其包含在您的 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>

By default the android Gradle plugin appends .test to the applicationId and namespace of the test app.默认情况下,android Gradle 插件将.test附加到测试应用程序的applicationIdnamespace So that you need to put all your classes in a separate test package.因此,您需要将所有类放在单独的测试 package 中。

  1. Change android test namespace and the applicationId .更改 android 测试namespaceapplicationId

You can define namespace and applicationId of the test app in module level build.gradle file.您可以在模块级build.gradle文件中定义测试应用程序的namespaceapplicationId Set it to match your test app's package name.将其设置为与您的测试应用程序的 package 名称相匹配。

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

It is recommended to use same values for the namespace and the applicationId .建议对namespaceapplicationId使用相同的值。 If you use different values, the android Gradle plugin will copy applicationId in place of namespace in the android manifest.如果您使用不同的值,android Gradle 插件将复制applicationId代替 android 清单中的namespace

You are running it as an Android Project, try running it as a Android Test Project 您将其作为Android项目运行,尝试将其作为Android测试项目运行

adb shell am instrument -e package <java package> -w <test apk package>/android.test.InstrumentationTestRunner adb shell am instrument -e package <java package> -w <test apk package> /android.test.InstrumentationTestRunner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 仪器测试:没有仪器注册错误 - Android Instrumentation Testing: No instrumentation registered Error 从Android中的Activity启动服务时出现“无法启动服务Intent”错误 - “Unable to start service Intent” error when starting service from an Activity in Android 当数据通过有意图的活动传递时,android 是如何工作的? - How android works when data is passed through activities with intent? 来回移动活动时Android意图返回null - Android intent returning null when moving back and forth of activities 无法以意图启动活动 - Unable to start activity with intent 打算进行其他活动的Android Studio - Android Studio with intent to other activities 有没有一种方法可以明确地告诉 android 系统将我的意图解析为当前意图操作的默认应用程序/活动? - Is there a way to explicitly say to android system resolve my intent to default app/activity for current intent action? Android Instrumentation 测试 - UI 线程问题 - Android Instrumentation Testing - UI Thread Issues 获取Android Instrumentation Test上的当前活动 - Get current Activity on Android Instrumentation Test 在浏览器意图android中打开网址后无法返回活动? - Unable to go back to activity after opening url in browser intent android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM