我正在使用Kotlin(Jetpack Compose)开发一个简单的应用程序,在解决运行应用程序时出现的问题时遇到了麻烦。从我个人的理解,这是不是那么好,在我看来,功能应该工作的预期,但我仍然不确定是否有一个问题,在代码本身或依赖关系。根据logcat,似乎我试图在MainActivity中声明的数据库没有被识别/创建。我试图通过使用ChatGPT处理依赖关系来解决这个问题,但我只遇到了更多的错误来修复,这个错误已经花了一段时间。当我尝试运行应用程序时,它会崩溃。如果有人知道如何解决这个问题,我将不胜感激。
- 数据库屏幕**
@Composable fun DatabaseScreen( navController: NavController, sliderValue: Float, userRepository: UserRepository ) { var userCount by remember { mutableStateOf(0) } var resultText by remember { mutableStateOf("") }
Column(
modifier = Modifier
.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
onClick = {
CoroutineScope(Dispatchers.IO).launch {
val startTime = System.nanoTime()
for (i in 0 until (sliderValue * sliderValue * 10).toInt()) {
val age = i % 17 + 1
userRepository.insertUser(User(age = age))
}
val endTime = System.nanoTime()
val duration = endTime - startTime
// Update UI on the main thread
withContext(Dispatchers.Main) {
resultText = "Added users: ${(sliderValue * sliderValue * 10).toInt()} in $duration nanoseconds"
}
}
}
}
- 数据库类和函数**
@Entity(tableName = "user")
data class User(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val age: Int
)
@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
@Dao
interface UserDao {
@Insert
suspend fun insertUser(user: User)
@Query("SELECT * FROM user")
suspend fun getAllUsers(): List<User>
@Query("DELETE FROM user")
suspend fun clearDatabase()
}
class UserRepository(private val userDao: UserDao) {
suspend fun insertUser(user: User) {
userDao.insertUser(user)
}
suspend fun getAllUsers(): List<User> {
return userDao.getAllUsers()
}
suspend fun clearDatabase() {
userDao.clearDatabase()
}
}
- 主要活动**
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val database = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java,
"my_database"
).build()
val userRepository = UserRepository(database.userDao())
setContent {
var navController = rememberNavController()
NavHost(navController = navController, startDestination = "mainScreen") {
composable("mainScreen") { MyApp(navController) }
composable("Screen0") {
UIScreen(navController, 5f)
}
composable("Screen1") {
AnimationScreen(navController, 5f)
}
composable("Screen2") {
ArithmeticLogicalScreen(navController, 4f)
}
composable("Screen3") {
DatabaseScreen(navController, 5f,userRepository)
}
composable("Screen4") {
FeaturesScreen(navController, 5f)
}
}
}
}
- 酒店LOGCAT**
致命异常:主要工艺:com. example. jurajappjetpack,PID:9583 java. lang. RuntimeException:无法启动活动PinterentInfo {com. example. jurajappjetpack/com. example. jurajappjetpack. MainActivity}:java.lang.RuntimeException:找不到com.example.jurajappjetpack.MainActivity.AppDatabase的实现。您访问的页面不存在! app. ActivityThread. performLaunchActivity(ActivityThread.java:2913)at android. app. ActivityThread.在android上安装LaunchActivity(ActivityThread.java:3048)。app. servertransaction。LaunchActivityItem。execute(LaunchActivityItem.java:78)at android. app. servertransaction。TransactionExecutor。android上的executeCallbacks(transactionExecutor.java:108)。app. servertransaction。TransactionExecutor。execute(transactionExecutor.java:68)at android. app. ActivityThread $H。在android上查看消息(ActivityThread.java:1808)。骨密度android上的dispatchMessage(Handler.java:106)。os. Looper.循环(Looper.java:193)在android. app. ActivityThread. main(ActivityThread.java:6669). lang。反思。方法。在com上调用(本地方法)。机器人内部的OS. RuntimeInit $ www.example.com 找不到com.example.jurajappjetpack.MainActivity.AppDatabase的实现。您访问的页面不存在!房间。房间。getGeneratedImplementation(Room. KT:58)在androidx上。房间。RoomDatabase $www.example.com(RoomDatabase.第1357章在一起举个例子。jurajappjetpack.主要活动。onCreate(MainActivity. KT:125)在Android上。应用程序。活动。performCreate(Activity.java:7136)at android.应用程序。活动。performCreate(Activity.java:7127)at android. app.仪器 Jmeter 。callActivityOnCreate(Instrumentation.java:1271)at android. app. ActivityThread. performLaunchActivity(ActivityThread.java:2893)at android. app. ActivityThread.在android上安装LaunchActivity(ActivityThread.java:3048)。app. servertransaction。LaunchActivityItem。execute(LaunchActivityItem.java:78)at android. app. servertransaction。TransactionExecutor。android上的executeCallbacks(transactionExecutor.java:108)。app. servertransaction。TransactionExecutor。execute(transactionExecutor.java:68)at android. app. ActivityThread $H。在android上查看消息(ActivityThread.java:1808)。骨密度android上的dispatchMessage(Handler.java:106)。os. Looper.循环(Looper.java:193)在android. app. ActivityThread. main(ActivityThread.java:6669). lang。反思。方法。在com上调用(本地方法)。机器人内部的OS. RuntimeInit $www.example.com(RuntimeInit.java:493)at com. android. internal. os. ZygoteInit. main(ZygoteInit.java:858)
- BUILD. GRADLE项目级别**
plugins { id("com.android.application") version "8.1.1" apply false
id("org.jetbrains.kotlin.android") version "1.8.10" apply false }
- 酒店BUILD. GRADLE MONTHLE LEVEL,巴塞罗那**
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.example.jurajappjetpack"
compileSdk = 33
defaultConfig {
applicationId = "com.example.jurajappjetpack"
minSdk = 28
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
release {
isMinifyEnabled = 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.4.3"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.0")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("com.google.android.material:material:1.9.0")
implementation("androidx.room:room-ktx:2.5.2")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
implementation ("androidx.navigation:navigation-compose:2.4.0")
implementation("androidx.compose.animation:animation-core")
implementation ("com.google.android.gms:play-services-location:18.0.0")
implementation ("androidx.activity:activity-ktx:1.3.1")
implementation ("androidx.fragment:fragment-ktx:1.4.0")
implementation ("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0")
implementation ("androidx.camera:camera-camera2:1.1.0-beta01")
implementation ("androidx.camera:camera-lifecycle:1.1.0-beta01")
implementation ("androidx.room:room-runtime:2.5.2")
annotationProcessor ("androidx.room:room-compiler:2.5.2")
}
1条答案
按热度按时间odopli941#
首先将kapt插件添加到模块构建gradle中,例如。(适用于所有3个插件):-
然后将
annotationProcessor ("androidx.room:room-compiler:2.5.2")
更改为annotationProcessor用于处理java kapt或ksp用于Kotlin。它是生成Java代码的处理器,因此代码不存在。