- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遵循了很多教程/文章和 googles architecture sample
无论我尝试什么,我都会收到一条错误消息,提示我需要将 Fragment 附加到带@AndroidEntryPoint 注释的 Activity。我已正确设置所有内容,但仍然无法正常工作。
java.lang.IllegalStateException: Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: class com.nikolam.colorme.HiltTestActivity
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:64)
at com.nikolam.main_feature.presenter.main_screen.Hilt_MainFragment.generatedComponent(Hilt_MainFragment.java:80)
at com.nikolam.main_feature.presenter.main_screen.Hilt_MainFragment.inject(Hilt_MainFragment.java:102)
at com.nikolam.main_feature.presenter.main_screen.Hilt_MainFragment.initializeComponentContext(Hilt_MainFragment.java:63)
at com.nikolam.main_feature.presenter.main_screen.Hilt_MainFragment.onAttach(Hilt_MainFragment.java:55)
at androidx.fragment.app.Fragment.onAttach(Fragment.java:1783)
at com.nikolam.main_feature.presenter.main_screen.Hilt_MainFragment.onAttach(Hilt_MainFragment.java:45)
at androidx.fragment.app.Fragment.performAttach(Fragment.java:2911)
at androidx.fragment.app.FragmentStateManager.attach(FragmentStateManager.java:464)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:275)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNow(BackStackRecord.java:305)
at com.nikolam.colorme.main_feature.MainFragmentTest$whenMainActivityLaunchedNavigatorIsInvokedForFragment$$inlined$launchFragmentInHiltContainer$1.perform(HiltExt.kt:46)
at androidx.test.core.app.ActivityScenario.lambda$onActivity$2$ActivityScenario(ActivityScenario.java:660)
at androidx.test.core.app.ActivityScenario$$Lambda$4.run(Unknown Source)
at androidx.test.core.app.ActivityScenario.onActivity(ActivityScenario.java:670)
at com.nikolam.colorme.main_feature.MainFragmentTest.whenMainActivityLaunchedNavigatorIsInvokedForFragment(MainFragmentTest.kt:85)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at dagger.hilt.android.internal.testing.MarkThatRulesRanRule$1.evaluate(MarkThatRulesRanRule.java:106)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:263)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
现在这是我的设置。
依赖
testImplementation(TestLibraryDependency.HILT_ANDROID_TESTING)
kaptTest(TestLibraryDependency.HILT_ANDROID_TESTING_COMPILER)
testImplementation(TestLibraryDependency.TEST_RUNNER)
testImplementation(TestLibraryDependency.ESPRESSO_CORE)
testImplementation(TestLibraryDependency.ANDROIDX_TEST_RULES)
testImplementation(TestLibraryDependency.ANDROIDX_CORE_TESTING)
testImplementation(TestLibraryDependency.ANDROIDX_TEST_EXT)
这是我的测试
@HiltAndroidTest
@Config(application = HiltTestApplication::class, maxSdk = Build.VERSION_CODES.P)
@RunWith(RobolectricTestRunner::class)
class MainFragmentTest {
@get:Rule()
var hiltAndroidRule = HiltAndroidRule(this)
@Before
fun init() {
hiltAndroidRule.inject()
}
@Test
fun whenMainActivityLaunchedNavigatorIsInvokedForFragment() {
// launchActivity()
// GIVEN - On the home screen
val navController = mock(NavController::class.java)
var fragment = launchFragmentInHiltContainer<MainFragment>() {
Navigation.setViewNavController(this.view!!, navController)
}
// WHEN - Click on the "+" button
onView(withId(R.id.add_floating_action)).perform(ViewActions.click())
// THEN - Verify that we navigate to the add screen
verify(navController).navigate(
Uri.parse(UPLOAD_DEEPLINK)
)
}
Launch in hilt container per google
inline fun <reified T : Fragment> launchFragmentInHiltContainer(
fragmentArgs: Bundle? = null,
@StyleRes themeResId: Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
crossinline action: Fragment.() -> Unit = {}
) {
val startActivityIntent = Intent.makeMainActivity(
ComponentName(
ApplicationProvider.getApplicationContext(),
HiltTestActivity::class.java
)
).putExtra(EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY, themeResId)
ActivityScenario.launch<HiltTestActivity>(startActivityIntent).onActivity { activity ->
val fragment: Fragment = activity.supportFragmentManager.fragmentFactory.instantiate(
Preconditions.checkNotNull(T::class.java.classLoader),
T::class.java.name
)
fragment.arguments = fragmentArgs
activity.supportFragmentManager
.beginTransaction()
.add(android.R.id.content, fragment, "")
.commitNow()
fragment.action()
}
}
(I also have their custom runner)
I also have a debug source set that contains HiltTestActivity like here, alongside debug manifest.
除此之外,我想以某种方式在我的 Activity 中注入(inject)和初始化我的 NavigationManager(围绕 navController 的包装类)。有没有办法做到这一点?当我尝试使用我的 MainActivity 进行测试时,我不断收到错误消息,因为我的 lateinit @Injects 没有被初始化...
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding;
private lateinit var navController : NavController
@Inject
lateinit var navManager: NavManager
private fun initNavManager() {
navManager.setOnNavEvent {
navController.navigate(it)
}
最佳答案
在我的例子中,我不得不使用
hilt {
enableTransformForLocalTests = true
}
由于 Roboelectric/Hilt 目前正在跟踪 20 个错误,因此很难确定是什么解决方法:)
关于android - 测试时,Hilt Fragment 必须附加到@AndroidEntryPoint Activity (它附加到@AndEntPoint 标记的 Activity ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66764260/
我在BottomSheetDialogFragmet() 中使用ViewModel,所以我必须用@AndroidEntryPoint 标记我的BottomSheet。 @AndroidEntryPoi
所以我的问题是,当我开始使用 Hilt 时,我们是否需要使用 @AndroidEntryPoint 注释标记所有 Activity ,或者我们是否可以创建一个 BaseActivity 并将其扩展到
当我通过 Room 在 android 中使用 Hilt 时我得到了这个错误。 完整的日志在这里: home/someone/Desktop/Calculator/app/build/tmp/kapt
是否必须在所有依赖类上添加 @AndroidEntryPoint 注释,例如依赖于 Activity 的 fragment 。是否有任何替代解决方案可以克服此异常? 最佳答案 只需添加 @Androi
我正在尝试使用 viewmodel 并观察 DialogFragment 中的数据,我也在注入(inject) ViewModel,同时实现代码出错。 PFB 我的代码 fragment 。并帮助我找
我遵循了很多教程/文章和 googles architecture sample 无论我尝试什么,我都会收到一条错误消息,提示我需要将 Fragment 附加到带@AndroidEntryPoint
运行单元测试代码时出现以下错误。 Caused by: java.lang.IllegalStateException: Hilt Fragments must be attached to an @
我是一名优秀的程序员,十分优秀!