无法创建 ViewModel 的实例

Cannot create an instance of ViewModel

我正在使用 ViewModel、Compose 和 Hilt 构建项目。我无法切换到 BookListScreen,因为我在标题中遇到错误。我认为重要的几点:

MainActivity.

主要Activity:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            LOTRTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    val navController = rememberNavController()
                    NavHost(
                        navController = navController,
                        startDestination = Screen.MainScreen.route
                    ) {
                        composable(
                            route = Screen.MainScreen.route
                        ) {
                            MainScreen(navController)
                        }
                        composable(
                            route = Screen.BookListScreen.route
                        ) {
                            BookListScreen(navController)
                        }
                    }
                }
            }
        }
    }
}

主屏幕:

@Composable
fun MainScreen(navController: NavController) {
    val configuration = LocalConfiguration.current
    when (configuration.orientation) {
        Configuration.ORIENTATION_LANDSCAPE -> {
            SectionsScreenRotate(navController)
        }
        else -> {
            SectionsScreen(navController)
        }
    }
}

@Composable
fun SectionsScreen(navController: NavController) {
    Column(
        modifier = Modifier.padding(10.dp)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .padding(10.dp)
        ) {
            SectionItem(
                section = Section("Characters", R.drawable.charachtersdraw),
                onItemClick = {}
            )
        }

        Column(
            modifier = Modifier
                .fillMaxHeight()
                .weight(1f)
                .padding(10.dp)
        ) {
            SectionItem(
                section = Section("Movies", R.drawable.moviesdraw),
                onItemClick = {}
            )
        }
        Column(
            modifier = Modifier
                .fillMaxHeight()
                .weight(1f)
                .padding(10.dp)
        ) {
            SectionItem(
                section = Section("Books", R.drawable.booksdraw),
                onItemClick = { navController.navigate(Screen.BookListScreen.route) }
            )
        }
    }
}

@Composable
fun SectionsScreenRotate(navController: NavController) {
    Row(
        modifier = Modifier.padding(10.dp)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .padding(10.dp)
        ) {
            SectionItem(
                section = Section("Characters", R.drawable.charachtersdraw),
                onItemClick = {}
            )
        }

        Column(
            modifier = Modifier
                .fillMaxHeight()
                .weight(1f)
                .padding(10.dp)
        ) {
            SectionItem(
                section = Section("Movies", R.drawable.moviesdraw),
                onItemClick = { navController.navigate(Screen.BookListScreen.route)}
            )
        }
        Column(
            modifier = Modifier
                .fillMaxHeight()
                .weight(1f)
                .padding(10.dp)
        ) {
            SectionItem(
                section = Section("Books", R.drawable.booksdraw),
                onItemClick = {}
            )
        }
    }

}

@Composable
fun SectionItem(section: Section, onItemClick: () -> Unit) {
    Card(
        modifier = Modifier
            .fillMaxSize()
            .clickable { onItemClick() },
        shape = RoundedCornerShape(15.dp),
        elevation = 5.dp,
    ) {
        Box(modifier = Modifier.fillMaxSize()) {
            Image(
                painter = painterResource(id = section.imageId),
                contentDescription = section.title,
                contentScale = ContentScale.Crop,
                modifier = Modifier.fillMaxSize()
            )
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(12.dp),
                contentAlignment = Alignment.BottomCenter
            ) {
                Text(
                    section.title, style = TextStyle(
                        color = Color.White,
                        fontSize = 16.sp,
                        fontWeight = FontWeight.Bold,
                    )

                )
            }

        }

    }
}

BookListScreen:

@Composable
fun BookListScreen(
    navController: NavController,
    bookListViewModel: BookListViewModel = viewModel()
) {
    val state = bookListViewModel.state.value

    Box(modifier = Modifier.fillMaxSize()) {
        LazyColumn(modifier = Modifier.fillMaxSize()) {
            items(state.books) { book ->
                BookListItem(
                    book = book
                )
            }
        }
        if (state.error.isNotBlank()) {
            Text(
                text = state.error,
                color = MaterialTheme.colors.error,
                textAlign = TextAlign.Center,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp)
                    .align(Alignment.Center)
            )
        }
        if (state.isLoading) {
            CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
        }
    }
}

ViewModel:

@HiltViewModel
class BookListViewModel @Inject constructor(
    private val bookRepository: BookRepository
) : ViewModel(){

    val state = mutableStateOf(BookListState())

    init {
        getBooks()
    }

    private fun getBooks() {
        bookRepository.getBooks().onEach { result ->
            when(result) {
                is Resource.Success -> {
                    state.value = BookListState(books = result.data ?: emptyList())
                }
                is Resource.Error -> {
                    state.value = BookListState(error = result.message ?: "Beklenmedik bir hata oluştu")
                }
                is Resource.Loading -> {
                    state.value = BookListState(isLoading = true)
                }
            }
        }.launchIn(viewModelScope)
    }

}

AppModule:

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Provides
    @Singleton
    fun getLotrApi() : LotrApi {
        return Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(LotrApi::class.java)
    }

    @Provides
    @Singleton
    fun getBookRepository(api: LotrApi) : BookRepository {
        return BookRepository(api)
    }
}

图书资料库:

    class BookRepository @Inject constructor(
    private val api : LotrApi
){

    fun getBooks() : Flow<Resource<List<Book>>> = flow {
        try {
            emit(Resource.Loading<List<Book>>())
            val books = api.getBookList().BookListDtoToBookList()
            emit(Resource.Success<List<Book>>(books))
        }
        catch (e: HttpException) {
            emit(Resource.Error<List<Book>>(e.localizedMessage ?: "Beklenmedik hata"))
        }
        catch(e: IOException) {
            emit(Resource.Error<List<Book>>(e.localizedMessage ?: "Lütfen internet bağlantınızı kontrol edin"))
        }

    }
}

屏幕:

sealed class Screen(val route : String){
    object BookListScreen : Screen("book_list")
    object BookChaptersScreen : Screen("book_chapters")
    object MainScreen : Screen("main_screen")
}

在将 Hilt 与导航库一起使用时,您需要使用 hiltViewModel() 而不是 viewModel() 来注入 ViewModel。查看 documentation 了解更多信息。