gpt4 book ai didi

android - 为什么 android jetpack compose 显示 "List contains no element matching the predicate."?

转载 作者:行者123 更新时间:2023-12-02 18:08:57 24 4
gpt4 key购买 nike

我的项目有入门屏幕,它工作正常,但我只想向用户展示一次,并且我使用了它的共享首选项,在调试项目时,它会抛出类似 logcat 中的错误,

List contains no element matching the predicate.

,在android模拟器中我可以看到主屏幕,但应用程序崩溃很快,当我在模拟器上再次打开应用程序时,登录屏幕仅显示1秒并打开主屏幕,而不是应用程序崩溃,我不知道为什么?我猜问题是因为这行代码,

if (!sharedPreferences.getBoolean("firstTime", true)) {
navController.navigate("main")
}else{

但我不确定。

主屏幕:

@Composable
fun MainScreen(
navController: NavController,

) {


Column(
Modifier
.fillMaxSize()

,
horizontalAlignment = Alignment.CenterHorizontally
) {

Text(
"WELCOME TO",
modifier = Modifier
.width(300.dp)

,
textAlign = TextAlign.Start,
fontSize = 15.sp,
color = custom,
fontWeight = FontWeight.Medium

)
}

导航:

@OptIn(ExperimentalPagerApi::class)
@Composable
fun NavScreen(
sharedPreferences: SharedPreferences
) {


val navController = rememberNavController()


NavHost(navController = navController, startDestination = "onBoard") {

composable("onBoard", ) {

OnBoardScreen(navController = navController, sharedPreferences)
}
composable("main") {
MainScreen(navController = navController)
}
}

View 模型:

class OnBoardViewModel (

) : ViewModel() {
private val _currentPage = MutableStateFlow(0)
val currentPage: StateFlow<Int> get() = _currentPage

fun setCurrentPage(currentPage: Int) {
_currentPage.value = currentPage
}

}

主要 Activity :

@AndroidEntryPoint
class MainActivity : ComponentActivity(

) {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContent {
CustomTheme {
Surface(color = MaterialTheme.colors.background) {
val sharedPreferences = getSharedPreferences("onBoardingScreen", MODE_PRIVATE)

NavScreen(sharedPreferences)
}
}
}
}
}



@ExperimentalPagerApi
@Composable
fun OnBoardScreen(
navController: NavController,
sharedPreferences: SharedPreferences
) {
val scaffoldState = rememberScaffoldState()
val onBoardViewModel : OnBoardViewModel = viewModel()
val currentPage = onBoardViewModel.currentPage.collectAsState()


val pagerState = rememberPagerState(
pageCount = onBoardItem.size,
initialOffscreenLimit = 2,
initialPage = 0,
infiniteLoop = false
)

Scaffold(
modifier = Modifier.fillMaxSize(),
scaffoldState = scaffoldState
) {
Surface(
modifier = Modifier.fillMaxSize()
) {

if (!sharedPreferences.getBoolean("firstTime", true)) {
navController.navigate("main")
}else{
val editor = sharedPreferences.edit()
editor.putBoolean("firstTime", false)
editor.apply()
}

LaunchedEffect(Unit) {
onBoardViewModel.currentPage
.collect {
pagerState.animateScrollToPage(
page = currentPage.value
)
}
}

Box(
modifier = Modifier
.fillMaxWidth()
.background(
brush = Brush.verticalGradient(
colors = listOf(
white,
grey
)
)
)
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally
) {
HorizontalPager(
state = pagerState
) { page ->
Column(
modifier = Modifier
.padding(top = 60.dp)
.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {

Text(
text = onBoardItem[page].desc,
modifier = Modifier
.padding(30.dp),
color = custom,
fontSize = 18.sp,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.padding(10.dp))

Image(
painter = painterResource(id = onBoardItem[page].image),
contentDescription = "OnBoardImage",
modifier = Modifier
.height(500.dp)
.fillMaxWidth(),

)

}
}

PagerIndicator(onBoardItem.size, pagerState.currentPage)
}

Box(
modifier = Modifier
.align(Alignment.BottomCenter)
) {
Row(
modifier = Modifier
.padding(bottom = 30.dp)
.fillMaxWidth(),
horizontalArrangement = if (pagerState.currentPage != 2 ) {
Arrangement.SpaceBetween
} else {
Arrangement.Center
}
) {
if (pagerState.currentPage == 2) {

CompositionLocalProvider(LocalRippleTheme provides NoRippleTheme) {
Text(
text = "Start",
color = custom,
modifier = Modifier
.padding(start = 280.dp, bottom = 40.dp)

.clickable {
navController.navigate("main")


},
fontSize = 18.sp,
fontWeight = FontWeight.Medium


)
}

} else {
CompositionLocalProvider(LocalRippleTheme provides NoRippleTheme) {
Text(
text = "Skip",
color = custom,
modifier = Modifier
.padding(start = 25.dp, bottom = 42.dp)
.clickable {
navController.navigate("main")

},
fontSize = 18.sp,
fontWeight = FontWeight.Medium
)
}

CompositionLocalProvider(LocalRippleTheme provides NoRippleTheme) {
Text(
text = "Next",
color = custom,
modifier = Modifier
.clickable {
onBoardViewModel.setCurrentPage(pagerState.currentPage + 1)
}
.padding(end = 25.dp, bottom = 42.dp),
fontSize = 18.sp,
fontWeight = FontWeight.Medium
)
}
}
}
}
}
}
}
}

@Composable
fun PagerIndicator(size: Int, currentPage: Int) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.padding(top = 40.dp)
) {
repeat(size) {
IndicateIcon(
isSelected = it == currentPage
)
}
}
}

@Composable
fun IndicateIcon(isSelected: Boolean) {
val width = animateDpAsState(
targetValue = if (isSelected) 10.dp else 10.dp
)

Box(
modifier = Modifier
.padding(2.dp)
.height(10.dp)
.width(width.value)
.clip(CircleShape)
.background(
if (isSelected) custom else customColor
)
)
}

最佳答案

我认为问题在于您在组合过程中调用 navController.navigate("main") 而没有确保在导航应该发生时仅调用一次,从而造成 navController.navigate("main") 被快速连续地重复调用。我认为这可能是原因,异常(exception)可能是结果。

将导航调用(现在是合成的直接一部分)包装到 LaunchedEffect 中

    if (!sharedPreferences.getBoolean("firstTime", true)) {
LaunchedEffect(Unit) {
navController.navigate("main")
}
} else {
val editor = sharedPreferences.edit()
editor.putBoolean("firstTime", false)
editor.apply()
}

关于android - 为什么 android jetpack compose 显示 "List contains no element matching the predicate."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72701963/

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