大家好,我正在尝试将我的Android Studio项目从NavHost
迁移到AnimatedNavHost
,因此我进行了以下更改:
- 将rememberNavController()替换为rememberAnimatedNavController()
- 将NavHost替换为AnimatedNavHost在此博客上找到:https://medium.com/androiddevelopers/animations-in-navigation-compose-36d48870776b
我没有添加任何动画,但当我尝试运行应用时,编译器告诉我以下错误:
java.lang.NoSuchMethodError:没有静态方法rememberNavController(Landroidx/compose/runtime/Composer;I)Landroidx/导航/导航主机控制器;Landroidx/导航/组合/导航主机控制器Kt类;或其超类(“androidx.navigation.compose.NavHostControllerKt”的声明出现在/data/app/~~ 3ss 2A 6 J7 mK 0 SYONGYPJOUw ==/com.示例.soschool-o-E9 YEHWVSYOUf 5 K7 L3 c5 Q ==/base.apk!类11.dex中)
现在我将附加两个感兴趣的类:
导航图:
@ExperimentalAnimationApi
@ExperimentalPagerApi
@Composable
fun SetupNavGraph(
navController: NavHostController,
startDestination: String
) {
AnimatedNavHost(
navController = navController,
startDestination = startDestination
) {
composable(route = Screen.Welcome.route) {
WelcomeScreen(navController = navController)
}
composable(route = Screen.Home.route) {
HomeScreen()
}
composable(route = Screen.Login.route){
LoginScreen()
}
}
}
主要活动:
@ExperimentalAnimationApi
@ExperimentalPagerApi
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject
lateinit var splashViewModel: SplashViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen().setKeepOnScreenCondition {
!splashViewModel.isLoading.value
}
setContent {
SOSchoolTheme {
val screen by splashViewModel.startDestination
val navController = rememberAnimatedNavController()
SetupNavGraph(navController = navController, startDestination = screen)
}
}
}
}
当我在没有Animated
的情况下运行应用程序时,一切都很好,但当我在MainActivity
上将AnimatedNavController()
更改为rememberAnimatedNavController()
,将NavHost
更改为AnimatedNavHost
时,应用程序崩溃并显示我写的错误。
4条答案
按热度按时间vwkv1x7d1#
将版本更改为0.21.1-beta对我很有效。
实现“com.google.伴奏者:伴奏者-导航-动画:0.21.1-beta”
rjjhvcjd2#
将导入从
import androidx.navigation.compose.composable
替换为import com.google.accompanist.navigation.animation.composable
。这是伴奏者github上列出的问题之一。plicqrtu3#
设置
compileSdkVersion
为33或最新的一个较高的所有需要的变化,如正确的导入和你阅读集成动画在您的应用程序。e37o9pze4#
升级到0.28.0对我很有效:
然后按照这个动画示例应用程序:
https://github.com/google/accompanist/blob/main/sample/src/main/java/com/google/accompanist/sample/navigation/animation/AnimatedNavHostSample.kt