JetPack Components Bugs 记录

JetPack Components Bugs

1.Room Persistence: Error:Entities and Pojos must have a usable public constructor

stackoverflow.com/questions/4…

@Ignore 应该放到类中声明

error:
@Entity(tableName = "t_user")
data class User(
    @PrimaryKey @ColumnInfo(name = "uid") var uid: Long,
    @ColumnInfo(name = "first_name") var firstName: String?,
    @ColumnInfo(name = "last_name") var lastName: String?,
    @Ignore var picture: Bitmap? = null
)

success:
@Entity(tableName = "t_user")
data class User(
    @PrimaryKey @ColumnInfo(name = "uid") var uid: Long,
    @ColumnInfo(name = "first_name") var firstName: String?,
    @ColumnInfo(name = "last_name") var lastName: String?
) {
    @Ignore
    var picture: Bitmap? = null
    //or
    // constructor() : this(0, "", "")
}

2.Not sure how to convert a Cursor to this method's return type

val allUsers: LiveData<List<User>> = userDao.getAll()

error:
@Query("select * from t_user order by uid asc")
fun getAll(): MutableLiveData<List<User>>

success:
@Query("select * from t_user order by uid asc")
fun getAll(): LiveData<List<User>>

3.Android ViewModel has no zero argument constructor

stackoverflow.com/questions/4…

For lifecycle_version = '2.2.0' ViewProviders.of API is deprecated . It`s my situation :

class MainActivityViewModel(application: Application) : AndroidViewModel(application) {

    private var repository: UserRepository

    val allUsers: LiveData<List<User>>
......


error:
val userViewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)

success:
val factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application)
userViewModel = ViewModelProvider(this,factory).get(MainActivityViewModel::class.java)

4.Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.

改了 data class User 中的字段,但是没有更新 version

  • Primary key constraint on id is ignored when being merged into com.ando.jetpack.room.User

stackoverflow.com/questions/4…

An @Embedded field cannot contain Primary Key.

error

@Entity(tableName = "t_user")
data class User(
    @ColumnInfo(name = "uid") @PrimaryKey var uid: Long?,
    @Embedded var address: Address? = null
)

error also

@Entity(tableName = "t_user")
data class User(
    @ColumnInfo(name = "uid") @PrimaryKey var uid: Long?,
    @SuppressWarnings(RoomWarnings.PRIMARY_KEY_FROM_EMBEDDED_IS_DROPPED)
    @Embedded var address: Address? = null
)

success

@SuppressWarnings(RoomWarnings.PRIMARY_KEY_FROM_EMBEDDED_IS_DROPPED)
@Entity(tableName = "t_user")
data class User(
    @ColumnInfo(name = "uid") @PrimaryKey var uid: Long?,
    @Embedded var address: Address? = null
)
  • A field can be annotated with only one of the following:ColumnInfo,Embedded,Relation

error

@Entity(tableName = "t_user")
data class User(
    @ColumnInfo(name = "uid") @PrimaryKey var uid: Long?,
    @ColumnInfo(name = "address") @Embedded var address: Address? = null
)

success

@Entity(tableName = "t_user")
data class User(
    @ColumnInfo(name = "uid") @PrimaryKey var uid: Long?,
    @Embedded var address: Address? = null
)

5.There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: t_book)

stackoverflow.com/questions/5…

You should mention both the entities in your roomDatabase class.


@Database(entities = {BaseWordId.class, ABC.class}, version = VERSION_CODE, exportSchema = false) 
public abstract class YourDatabase extends RoomDatabase {
    //your Daos
}

6.The column songId in the junction entity com.ando.jetpack.room.PlaylistSongCrossRef is being used to resolve a relationship but it is not covered by any index.

This might cause a full table scan when resolving the relationship, it is highly advised to create an index that covers this column.

warn

@Entity(primaryKeys = ["playlistId", "songId"])
data class PlaylistSongCrossRef(
    val playlistId: Long,
    val songId: Long
)

no warn

@Entity(primaryKeys = ["playlistId", "songId"])
data class PlaylistSongCrossRef(
    val playlistId: Long,
    @ColumnInfo(index = true) val songId: Long
)

7.[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).

stackoverflow.com/questions/5…

禁用增量注解 : gradle.properties add kapt.incremental.apt=false

8.WARNING: DSL element 'android.viewBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.viewBinding'.

WARNING: DSL element 'android.viewBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.viewBinding'.
It will be removed in version 5.0 of the Android Gradle plugin.
WARNING: DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding'.
It will be removed in version 5.0 of the Android Gradle plugin.

app build.gradle 中开启 dataBinding 和 viewBinding方式将在 5.0 Gradle plugin 中移除:

viewBinding {
    enabled = true
}
dataBinding {
    enabled true
}

改为 :


buildFeatures {
    viewBinding = true
    dataBinding = true
}

9. Not sure how to convert a Cursor to this method's return type

blog.csdn.net/yuzhiqiang_…

如果在Room中方法返回值得类型定义为 LiveData 时,那么,该方法则默认是 异步 的:

官方文档

@Query("select * from t_article_tabs order by tabId asc")
suspend fun getAll():  MutableLiveData<List<WxArticleTabsEntity>>?

改为 :

@Query("select * from t_article_tabs order by tabId asc")
suspend fun getAll(): List<WxArticleTabsEntity>?

10. Entities and POJOs must have a usable public constructor.

stackoverflow.com/questions/5…

@Entity(tableName = "t_article_tabs")
data class WxArticleTabsEntity(
    @ColumnInfo(name = "tabId") @PrimaryKey @SerializedName("id") val id: Int?,
    @ColumnInfo(name = "tabName") @SerializedName("name") val name: String?,
    ...
    @Ignore  @SerializedName("children") val children: List<Any>?,
)

改为 :

@Entity(tableName = "t_article_tabs")
data class WxArticleTabsEntity(
    @ColumnInfo(name = "tabId") @PrimaryKey @SerializedName("id") val id: Int?,
    @ColumnInfo(name = "tabName") @SerializedName("name") val name: String?,
    ...
){
    @Ignore  @SerializedName("children") val children: List<Any>? = null
}

11. Application namespace for attribute bind:loadPic will be ignored.

stackoverflow.com/questions/3…

@BindingAdapter({"loadPic"})

xml :

<ImageView
    loadPic="@{viewModel.tempImageUrl}"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

12. Updating...

.

本文作者:风之旅人

本文链接:https://www.cnblogs.com/jooy/p/17302075.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   javakam  阅读(0)  评论(0编辑  收藏  举报  
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起