所以我有三个模块
包含BaseFragment
:features:home包含HomeFragment的:commons:ui,实现包含MainActivity的:app,实现:features:home现在,如果我尝试使用下面的代码在MainActivity中运行onCreate
supportFragmentManager
.beginTransaction()
.add(R.id.fragment_container, HomeFragment())
.commit()我知道错误了
e: Supertypes of the following classes cannot be resolved. Please make sure you have the required
dependencies in the classpath:
class tech.vrutal.home.HomeFragment, unresolved supertypes: tech.vrutal.ui.BaseFragment但是,如果我使用导航组件,如下所示(删除MainActivity中的fragmentManager代码)
// activity_main.xml
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
// nav_graph.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/home_fragment">
<fragment
android:id="@+id/home_fragment"
android:name="tech.vrutal.home.HomeFragment"
android:label="HomeFragment" />
</navigation>现在它按预期工作了。
那么,为什么直接在HomeFragment()中创建MainActivity失败了,而使用导航组件却很好
发布于 2021-06-09 08:55:29
我在一个非常相似的模块体系结构中面临着这个问题。
我通过更改:features:home的build.gradle文件中的依赖项声明来解决这个问题。
从…
dependencies {
//...
implementation project(":commons:ui")
//...
}至
dependencies {
//...
api project(":commons:ui")
//...
}发布于 2021-01-26 01:59:42
我在一天中多次犯同样的错误。我不知道原因,也不知道如何解决问题,但每次我第二次尝试运行这个应用程序时,它都能工作。
https://stackoverflow.com/questions/65696225
复制相似问题