尝试在ActivityUnitTestCase中启动活动时出现IllegalArgumentException

时间:2016-01-15 05:14:02

标签: java android unit-testing android-intent android-activity

我正在尝试为Android游戏中的特定活动编写单元测试。在每个测试中,我需要使用一个额外添加的Intent启动Activity,然后验证活动中的某个视图是否显示该Intent extra的正确内容。以下是一些代码,用于演示我正在谈论的内容:

package com.example.puzzlegame;

import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.MediumTest;


public class GameActivityTest extends ActivityUnitTestCase<GameActivity> {

    public GameActivityTest(){
        super(GameActivity.class);
    }

    @MediumTest
    public void testViews() {
        Intent activityLaunchIntent = new Intent(getInstrumentation().getTargetContext(), GameActivity.class);
        activityLaunchIntent.putExtra("extra1", "somevalue");
        startActivity(activityLaunchIntent, null, null);

        //Ensure that views in GameActivity contain correct data, run other tests, etc.
    } 

但是当我尝试运行测试时,我收到以下IllegalArgumentException。

java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.example.puzzlegame/com.example.puzzlegame.GameActivity}
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:284)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:152)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:60)
at com.example.puzzlegame.GameActivity.onCreate(GameActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:163)
at com.example.puzzlegame.GameActivityTest.testViews(GameActivityTest.java:29)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
Caused by: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.example.puzzlegame/com.example.puzzlegame.GameActivity}
at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:314)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:301)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:281)
... 20 more
}

stacktrace显示对startActivity()的调用导致了IllegalArgumentException,但我不确定原因。有人可以帮帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先,最重要的是你需要这样做而不是startActivity:希望这可以解决你的问题。

setActivity(launchActivity(pkg, actClass, null));

但问题仍然存在, 通过添加此

来设置packageName
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setPackage(APP_PACKAGE);
intent.setClassName(APP_PACKAGE, "your_package_name.Activity");
intent.setPackage(APP_PACKAGE);

让我知道它是否有效。