2012-01-13 84 views
4

我在尝试在android上运行测试测试时出现错误。我写了一个活动,名为AudioPlayerActivity,位于包com.mycompany.mobile.android.gui,现在我试图测试该项目的GUI,我在下面的错误中运行:无法解决以下活动:当仪器测试Android活动时的意图

java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.mycompany.mobile.android.gui/.AudioPlayerActivity }

我已阅读this question,并遵循建议有,但都无济于事。我也搜索了错误,但没有找到任何帮助。

这是我的测试项目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> 

这是我的仪器测试。

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()); 
    } 
} 

我们通过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 intent过滤器来实现所需的活动,但是这并没有改变测试的结果。我曾试着从事这些活动,没有他们,也没有改变结果。

回答

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

根据官方的开发者指南here,你的测试项目必须有一个不同的包的名字从你测试项目:

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.

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

+0

这现在是无效的。 – 2016-04-02 13:34:08

0

你运行它作为一个的Android项目,尝试运行它作为一个Android的测试项目

亚行外壳上午仪器-e包< java包> -w <测试APK包> /android.test.InstrumentationTestRunner

+0

我很伤心,我正在通过maven运行它。我粘贴了pom.xml [这里](http://pastie.org/private/a91ur2skgambknh2cryhba)。删除testSourceDirectory和sourceDirectory文件夹不会改变结果。 – wonderb0lt 2012-01-13 18:21:07