2012-01-13 32 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