我正在尝试编写Fragment的检测测试。我使用了stackoverflow中的示例代码。但是我找不到这个错误的原因:
java.lang.RuntimeException:无法解析以下活动:Intent {act = android.intent.action.MAIN flg = 0x10000000
互联网消息来源表明,它可能是由以下原因造成的:
但我不认为这是事实。错误:
started: testFragment(de.my.androidhd.test.AccountFragmentTest)
failed: testFragment(de.my.androidhd.test.AccountFragmentTest)
----- begin exception -----
java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=de.my.androidhd/.test.TestFragmentActivity }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:371)
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119)
at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97)
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104)
at de.my.androidhd.test.AccountFragmentTest.setUp(AccountFragmentTest.java:22)
at junit.framework.TestCase.runBare(TestCase.java:132)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at junit.framework.TestResult.runProtected(TestResult.java:133)
at junit.framework.TestResult.run(TestResult.java:118)
at junit.framework.TestCase.run(TestCase.java:124)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
----- end exception -----
finished: testFragment(de.my.androidhd.test.AccountFragmentTest)
我有正确的Manifest.xml:
<uses-sdk android:minSdkVersion="11" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="de.my.androidhd" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="de.my.androidhd.test.TestFragmentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<uses-library android:name="android.test.runner" />
</application>
测试代码:
package de.my.androidhd.test;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.test.ActivityInstrumentationTestCase2;
import de.my.androidhd.ui.AccountFragment;
public class AccountFragmentTest extends ActivityInstrumentationTestCase2<TestFragmentActivity> {
private TestFragmentActivity mActivity;
public AccountFragmentTest() {
super(TestFragmentActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
}
private Fragment startFragment(Fragment fragment) {
FragmentTransaction transaction = mActivity.getSupportFragmentManager().beginTransaction();
transaction.add(R.id.activity_test_fragment_linearlayout, fragment, "tag");
transaction.commit();
getInstrumentation().waitForIdleSync();
Fragment frag = mActivity.getSupportFragmentManager().findFragmentByTag("tag");
return frag;
}
public void testFragment() {
AccountFragment fragment = new AccountFragment() {
// Override methods and add assertations here.
};
Fragment frag = startFragment(fragment);
}
}
答案 0 :(得分:0)
所以问题是TestFragmentActivity,意思是我们测试中使用的Activity
extends ActivityInstrumentationTestCase2<TestFragmentActivity>
必须在Manifest.xml中定义的包中作为targetPackage提供:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="de.my.androidhd" />
我的解决方案是将TestFragmentActivity移动到经过测试的应用程序包中。