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

China☆狼群 提交于 2019-12-01 04:55:41

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/8854936/unable-to-resolve-activity-for-intent-when-instrumentation-testing-android-acti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!