gpt4 book ai didi

android - Hilt Fragments 必须附加到 @AndroidEntryPoint Activity。找到 : class androidx. fragment.app.testing.FragmentScenario$EmptyFragmentActivity

转载 作者:行者123 更新时间:2023-12-03 13:49:56 32 4
gpt4 key购买 nike

运行单元测试代码时出现以下错误。

Caused by: java.lang.IllegalStateException: Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: class androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity
at dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83)
at dagger.hilt.android.internal.managers.FragmentComponentManager.createComponent(FragmentComponentManager.java:75)
at dagger.hilt.android.internal.managers.FragmentComponentManager.generatedComponent(FragmentComponentManager.java:63)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.generatedComponent(Hilt_HomePage.java:70)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.inject(Hilt_HomePage.java:89)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.initializeComponentContext(Hilt_HomePage.java:53)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.onAttach(Hilt_HomePage.java:45)
at androidx.fragment.app.Fragment.onAttach(Fragment.java:1602)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.onAttach(Hilt_HomePage.java:35)
at com.zhixin.wedeep.homepage.ui.HomePage.onAttach(HomePage.kt:281)
这是我的测试代码。
@HiltAndroidTest
@UninstallModules(HomePageDataModule::class)
@RunWith(AndroidJUnit4::class)
@LargeTest
class TestHomePageFragment {

private val c = Composition("cyrus", "background", "description", "downloadUrl", "1000", "url", "1", true, "100", 100, "100", "test", "title", "1", "100", "cover", ArrayList(), "ONCE", null)

@Inject
lateinit var cpd: CompositionDao

@get:Rule
var hiltRule = HiltAndroidRule(this)




@Before
fun init() {
hiltRule.inject()
Util.RETROFIT

Util.enqueueResponse("mainpage.json")

cpd.createComposition(c)
cpd.createBrowseRecord(BrowseRecord(c.id, System.currentTimeMillis()))
val s = launchFragment<HomePage>()
s.onFragment {
IdlingRegistry.getInstance().register(it.mIdleResource)
}
/* dataBindingIdlingResourceRul = DataBindingIdlingResourceRule(s)
dataBindingIdlingResourceRul.starting(null)*/

}


@Test
fun testDataInitial() {

onView(ViewMatchers.withId(R.id.recycler_view_preview_data))
.perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(1))
}

@After
fun finish(){

}

}
对这个问题有任何想法吗?

最佳答案

Hilt guide 中所述,“测试”部分:

Warning: Hilt does not currently support FragmentScenario because there is no way to specify an activity class, and Hilt requires a Hilt fragment to be contained in a Hilt activity. One workaround for this is to launch a Hilt activity and then attach your fragment.


发生错误是因为 hilt 管理的 fragment 也应该附加到 hilt 管理的 Activity 上,简而言之,两者都应该用 @AndroidEntryPoint 注释.因为 FragmentScenario 使用 EmptyFragmentActivity 来保存正在测试的底层 Fragment,所以目前没有办法将 Hilt 与 FragmentScenario 集成。一种解决方法是启动一个 Activity ,然后将 fragment 附加到它。

关于android - Hilt Fragments 必须附加到 @AndroidEntryPoint Activity。找到 : class androidx. fragment.app.testing.FragmentScenario$EmptyFragmentActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63189892/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com