我正在编写一个小应用程序来记录每日事务,当我尝试创建一个房间数据库时,我跟着一些指南,然后切换变量以满足我的需要。事情是我得到了奇怪的Dao编译错误。
这是错误代码;
Executing tasks: [clean, :app:assembleDebug]
Configuration on demand is an incubating feature.
app: 'annotationProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.android.databinding:compiler:3.1.0'.
:clean
:app:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugResValues
:app:generateDebugResources
:app:mergeDebugResources
:app:transformDataBindingBaseClassLogWithDataBindingMergeGenClassesForDebug
:app:dataBindingGenBaseClassesDebug
:app:checkDebugManifest
:app:generateDebugBuildConfig
:app:prepareLintJar UP-TO-DATE
:app:mainApkListPersistenceDebug
:app:createDebugCompatibleScreenManifests
:app:processDebugManifest
:app:splitsDiscoveryTaskDebug
:app:processDebugResources
:app:generateDebugSources
:app:dataBindingExportBuildInfoDebug
:app:kaptGenerateStubsDebugKotlin
:app:transformDataBindingWithDataBindingMergeArtifactsForDebug
:app:kaptDebugKotlin
w: warning: Supported source version 'RELEASE_7' from annotation processor 'org.jetbrains.kotlin.kapt3.ProcessorWrapper' less than -source '1.8'
w: /Users/olivier/StudioProjects/BudgetProject/app/build/tmp/kapt3/stubs/debug/com/example/android/budgetproject/Transaction.java:7: warning: There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors.
public final class Transaction {
^
e: error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
e: /Users/olivier/StudioProjects/BudgetProject/app/build/tmp/kapt3/stubs/debug/com/example/android/budgetproject/TransactionDao.java:11: error: Not sure how to convert a Cursor to this method's return type
public abstract java.util.List<android.arch.persistence.room.Transaction> getAllTransaction();
^
w: /Users/olivier/StudioProjects/BudgetProject/app/build/tmp/kapt3/stubs/debug/com/example/android/budgetproject/TransactionDao.java:11: warning: The query returns some columns [depense, description, date, uid] which are not use by android.arch.persistence.room.Transaction. You can use @ColumnInfo annotation on the fields to specify the mapping. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: depense, description, date, uid. Fields in android.arch.persistence.room.Transaction: .
public abstract java.util.List<android.arch.persistence.room.Transaction> getAllTransaction();
^
w: /Users/olivier/StudioProjects/BudgetProject/app/build/tmp/kapt3/stubs/debug/com/example/android/budgetproject/TransactionDao.java:15: warning: The query returns some columns [depense, description, date, uid] which are not use by android.arch.persistence.room.Transaction. You can use @ColumnInfo annotation on the fields to specify the mapping. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: depense, description, date, uid. Fields in android.arch.persistence.room.Transaction: .
public abstract java.util.List<android.arch.persistence.room.Transaction> findTransactionById(@org.jetbrains.annotations.NotNull()
^
e: /Users/olivier/StudioProjects/BudgetProject/app/build/tmp/kapt3/stubs/debug/com/example/android/budgetproject/TransactionDao.java:15: error: Not sure how to convert a Cursor to this method's return type
public abstract java.util.List<android.arch.persistence.room.Transaction> findTransactionById(@org.jetbrains.annotations.NotNull()
^
e: /Users/olivier/StudioProjects/BudgetProject/app/build/tmp/kapt3/stubs/debug/com/example/android/budgetproject/TransactionDao.java:20: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
android.arch.persistence.room.Transaction transaction);
^
e: /Users/olivier/StudioProjects/BudgetProject/app/build/tmp/kapt3/stubs/debug/com/example/android/budgetproject/TransactionDao.java:24: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
android.arch.persistence.room.Transaction transaction);
^
w: /Users/olivier/StudioProjects/BudgetProject/app/build/tmp/kapt3/stubs/debug/com/example/android/budgetproject/MyDatabase.java:7: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
public abstract class MyDatabase extends android.arch.persistence.room.RoomDatabase {
^
:app:kaptDebugKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 11s
21 actionable tasks: 20 executed, 1 up-to-date
Dao代码
package com.example.android.budgetproject;
import android.arch.persistence.room.*
import android.arch.persistence.room.Transaction
import android.arch.persistence.room.Delete
import android.arch.persistence.room.Dao
@Dao
interface TransactionDao {
@Query("SELECT * FROM `transaction`")
fun getAllTransaction(): List<Transaction>
@Query("SELECT * FROM `transaction` where uid IN (:transactionId)")
fun findTransactionById(transactionId: IntArray): List<Transaction>
@Insert(onConflict = 1)
fun insertTransaction(transaction: Transaction)
@Delete
fun deleteTransaction(transaction: Transaction)
}
以防万一:
package com.example.android.budgetproject
import android.arch.persistence.room.ColumnInfo
import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey
@Entity(tableName = "transaction")
data class Transaction(
@ColumnInfo(name = "depense") var depense: String = "",
@ColumnInfo(name = "description") var description: String = "",
@ColumnInfo(name = "date") var date: String = "",
@PrimaryKey(autoGenerate = true) var uid: Long = 0)
此处还有我正在关注的指南之一的链接, https://medium.com/@ajaysaini.official/building-database-with-room-persistence-library-ecf7d0b8f3e9
希望我的问题不在我的清单中。但请告诉我,如果你认为这可能是一个错误的依赖,我将迅速将我的表现提交给这篇文章。
答案 0 :(得分:2)
在您的DAO中,您要导入此内容:
import android.arch.persistence.room.Transaction
您应该导入实体Transaction
而不是Room类。