所以我尝试但失败了......
按照以下方式使用您的代码和依赖项:-
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'androidx.room:room-ktx:2.4.0-beta02'
implementation 'androidx.room:room-runtime:2.4.0-beta02'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.4.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
kapt 'androidx.room:room-compiler:2.4.0-beta02'
kapt 'androidx.lifecycle:lifecycle-compiler:2.4.0'
}
然后它可以正常编译,并且在您的情况下出现乱码的 StudentDao_Impl 部分编译为 ):-
@Override
public List<Student> call() throws Exception {
final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
try {
final int _cursorIndexOfName = CursorUtil.getColumnIndexOrThrow(_cursor, "name");
final int _cursorIndexOfAge = CursorUtil.getColumnIndexOrThrow(_cursor, "age");
final int _cursorIndexOfGender = CursorUtil.getColumnIndexOrThrow(_cursor, "gender");
final int _cursorIndexOfId = CursorUtil.getColumnIndexOrThrow(_cursor, "id");
final List<Student> _result = new ArrayList<Student>(_cursor.getCount());
while(_cursor.moveToNext()) {
final Student _item;
final String _tmpName;
if (_cursor.isNull(_cursorIndexOfName)) {
_tmpName = null;
} else {
_tmpName = _cursor.getString(_cursorIndexOfName);
}
final int _tmpAge;
_tmpAge = _cursor.getInt(_cursorIndexOfAge);
final Gender _tmpGender;
_tmpGender = __Gender_stringToEnum(_cursor.getString(_cursorIndexOfGender));
_item = new Student(_tmpName,_tmpAge,_tmpGender);
final long _tmpId;
_tmpId = _cursor.getLong(_cursorIndexOfId);
_item.setId(_tmpId);
_result.add(_item);
}
return _result;
} finally {
_cursor.close();
}
}
@Override
protected void finalize() {
_statement.release();
}
});
大约 2/3 后是行 _item = new Student(_tmpName,_tmpAge,_tmpGender);
你的在这个阶段是乱码,但有趣的是显示 _item = new Student(); 好像 _tmpName _tmpAge 和 _tmpGender 不存在。
乱码数据就好像一个字节流破坏了生成的代码。
我建议尝试清理并重建,也许退出 Android Studio 并返回。您的代码似乎没有任何问题
----
附:您对 TypeConverters 发表评论说 我认为新版本默认会自动提供此功能...
你是对的,在 StudentDao_Impl 中它包括:-
private String __Gender_enumToString(final Gender _value) {
if (_value == null) {
return null;
} switch (_value) {
case MALE: return "MALE";
case FEMALE: return "FEMALE";
case UNKNOWN: return "UNKNOWN";
default: throw new IllegalArgumentException("Can't convert enum to string, unknown enum value: " + _value);
}
}
private Gender __Gender_stringToEnum(final String _value) {
if (_value == null) {
return null;
} switch (_value) {
case "MALE": return Gender.MALE;
case "FEMALE": return Gender.FEMALE;
case "UNKNOWN": return Gender.UNKNOWN;
default: throw new IllegalArgumentException("Can't convert value to enum, unknown value: " + _value);
}
}
附:我还将您的代码包含在另一个答案中,该答案已编译并使用 beta02 运行并显示(单击插入几次后):-
所以你的代码肯定没有问题。