我正在尝试制作一个带有API调用的应用程序,但是当我启动时,它因错误而崩溃,在互联网上进行了搜索,但找不到问题所在。 这里有一个错误:
*引起:java.lang.InstantiationException:java.lang.Class
我的代码:
DishApplication.kt:
package com.lobo
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class DishApplication:Application() {
}
api.models:
class DishList : ArrayList<DishListItem>()
data class DishListItem(
val activeCount: Int,
val actualPrice: Int,
val description: String,
val dishId: Int,
val isLobobox: Int,
val name: String,
val orders: List<Order>,
val originalPrice: Int,
val photo: String,
val tags: List<String>
)
data class Order(
val actualPrice: Int,
val customerId: String,
val orderId: Int,
val originalPrice: Int,
val status: String
)
api.requests.GetDishList:
import com.lobo.lobo.data.api.APIConstants
import retrofit2.http.GET
import com.lobo.lobo.data.api.models.DishList
import retrofit2.http.Header
interface GetDishList {
@GET(APIConstants.DISH_LIST)
suspend fun getDishes(@Header("Authorization") authHeader: String): DishList
}
API常量:
object APIConstants {
const val BASE_URL = "my_server_link"
const val DISH_LIST = "list_dishes"
}
数据.repository.DishRepo:
class DishRepo @Inject constructor(
private val dishList : GetDishList
){
suspend fun getDishList(authHeader: String) : DishList{
return dishList.getDishes(authHeader)
}
}
lobo.di.DishAPIModule:
@Module
@InstallIn(SingletonComponent::class)
object DishAPIModule {
@Provides
@Singleton
fun provideGetDishList(retrofit: Retrofit): GetDishList {
return retrofit.create(GetDishList::class.java)
}
@Provides
@Singleton
fun provideRetrofit(): Retrofit {
return Retrofit.Builder()
.baseUrl(APIConstants.BASE_URL)
.build()
}
}
lobo.ViewModels.DishesViewModel:
@HiltViewModel
class DishesViewModel @Inject constructor(
private val dishRepo: DishRepo
) : ViewModel() {
private val _dishes = MutableLiveData<DishList>()
val dishes: LiveData<DishList> = _dishes
fun getDishes(authHeader: String) {
viewModelScope.launch {
val dishList = dishRepo.getDishList(authHeader)
_dishes.postValue(dishList)
}
}
}
Dishes.kt(片段):
@Composable
fun ListOfDishesScreen(navController: NavController) {
var showDialog by rememberSaveable { mutableStateOf(false) }
var checkedState by remember { mutableStateOf(false) }
val viewModel: DishesViewModel = viewModel()
LaunchedEffect(Unit) {
viewModel.getDishes("Bearer my_token")
Log.d("WTF", "token send")
}
val dishes by viewModel.dishes.observeAsState()
//some code here
dishes?.forEach { dish ->
DishAPICard(
dishName = dish.name,
dishPhoto = dish.photo , // You will need to load the image from the URL.
actualPrice = dish.actualPrice,
navController = navController
)
Spacer(modifier = Modifier.height(16.dp))
}
MainActivity.kt:
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)
setContent {
val is_auth = false
val navController = rememberNavController()
NavHost(navController, startDestination = if (is_auth) "lol" else "dish_list") {
composable("dish_list"){ ListOfDishesScreen(navController = navController)}
还添加我的构建gradle文件(mb有一个依赖错误,我尝试了一切)
build.gradle(项目):
buildscript {
ext {
compose_ui_version = '1.2.0'
hilt_version = '2.43'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
id 'com.google.dagger.hilt.android' version '2.43' apply false
}
build.gradle(模块:应用程序):
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
apply plugin: 'kotlin-kapt'
android {
namespace 'com.lobo.lobo'
compileSdk 33
defaultConfig {
applicationId "com.lobo.lobo"
minSdk 29
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
implementation 'androidx.compose.material:material:1.2.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_ui_version"
implementation 'com.google.accompanist:accompanist-systemuicontroller:0.17.0'
implementation "com.google.accompanist:accompanist-insets:0.17.0"// Please use the latest version
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:2.5.3"
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.6.0"
implementation "io.coil-kt:coil-compose:1.3.2"
implementation 'com.google.dagger:dagger:2.43'
kapt 'com.google.dagger:hilt-compiler:2.43'
kapt 'com.google.dagger:dagger-compiler:2.43'
implementation "com.google.dagger:hilt-android:2.43"
kapt "com.google.dagger:hilt-android-compiler:2.43"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.activity:activity-compose:1.3.1' // Make sure you use the latest version
implementation 'androidx.activity:activity:1.3.1' // Make sure you use the latest version
}
我尝试更改依赖项和我的 @Composable 对象,但仍然出现相同的错误
我认为你需要改变
val viewModel: DishesViewModel = viewModel()
到
val viewModel: DishesViewModel = hiltViewModel()
在
Dishes.kt
。