获取 android 房间数据库 java.lang.IllegalArgumentException 的以下异常:@androidx.room.Database 未定义元素视图()
Posted
技术标签:
【中文标题】获取 android 房间数据库 java.lang.IllegalArgumentException 的以下异常:@androidx.room.Database 未定义元素视图()【英文标题】:Getting following exception for android room database java.lang.IllegalArgumentException: @androidx.room.Database does not define an element views() 【发布时间】:2020-03-15 05:21:12 【问题描述】:android 房间数据库出现以下异常
java.lang.IllegalArgumentException:@androidx.room.Database 没有 定义一个元素views()
我试图在 SO 上搜索任何解决方案,但找不到任何解决方案。 谁能解释这个错误与什么有关?
我只在我的房间数据库中使用 java。我没有使用 kotlin。 库版本
roomVersion = '2.2.1'
archLifecycleVersion = '2.1.0'
定义Entities
,Dao
和一个扩展roomdatabase的类
public static TncWalletAppDatabase getDatabase(final Context context)
if (INSTANCE == null)
synchronized (TncWalletAppDatabase.class)
if (INSTANCE == null)
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
TncWalletAppDatabase.class, "tnc_app_database")
.addCallback(sRoomDatabaseCallback)
.allowMainThreadQueries()
.build();
return INSTANCE;
这是我初始化数据库的方法。 非常感谢您对此的任何帮助。
数据库类:
@Database(entities = Merchant.class, MerchantProfile.class, Receipt.class, ReceiptItems.class,TncTransaction.class, version = 1)
public abstract class TncWalletAppDatabase extends RoomDatabase
private static TncWalletAppDatabase INSTANCE;
public abstract MerchantDao mMerchantDao();
public abstract MerchantProfileDao mMerchantProfileDao();
public abstract ReceiptDao mReceiptDao();
public abstract ReceiptItemsDao mReceiptItemsDao();
public abstract TncTransactionDao mTncTransactionDao();
public static TncWalletAppDatabase getDatabase(final Context context)
if (INSTANCE == null)
synchronized (TncWalletAppDatabase.class)
if (INSTANCE == null)
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
TncWalletAppDatabase.class, "tnc_app_database")
.addCallback(sRoomDatabaseCallback)
.allowMainThreadQueries()
.build();
return INSTANCE;
private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback()
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db)
super.onCreate(db);
// database trigger for tnc_transaction table
new CreateDatabaseTriggerOnTncTransaction().execute();
;
private static class CreateDatabaseTriggerOnTncTransaction extends AsyncTask<Void, Void, Void>
@Override
protected Void doInBackground(final Void... params)
/**
* Create database trigger when the tnc transaction table is created.
*/
SupportSQLiteDatabase supportSQLiteDatabase = INSTANCE.getOpenHelper().getWritableDatabase();
if( supportSQLiteDatabase != null )
String TABLE_NAME = "tnc_transaction";
supportSQLiteDatabase.execSQL(
"CREATE TRIGGER IF NOT EXISTS delete_" + TABLE_NAME + "_logs_max " +
"BEFORE INSERT ON " + TABLE_NAME + " " +
"WHEN (SELECT COUNT(*) FROM " + TABLE_NAME + ") > "
+ TncTransactionDao.MAX_ROW_COUNT +
" " + " BEGIN DELETE FROM " + TABLE_NAME + " WHERE id = "
+ "(SELECT MIN(id) FROM " + TABLE_NAME + "); " +
"END;");
return null;
【问题讨论】:
你能发一下你的TncWalletAppDatabase
课程吗?
@GiorgosNeokleous 刚刚发布了我的数据库课程。
@Ameya721 你好,你找到解决这个问题的方法了吗?在我的情况下,当我尝试将我的房间库从 2.0.0-alpha1 -> 更新到 2.2.3 时,会引发相同的错误
【参考方案1】:
将此添加到您的 build.grade 文件中?
dependencies
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
参考# https://developer.android.com/training/data-storage/room
【讨论】:
以上是关于获取 android 房间数据库 java.lang.IllegalArgumentException 的以下异常:@androidx.room.Database 未定义元素视图()的主要内容,如果未能解决你的问题,请参考以下文章
获取 android 房间数据库 java.lang.IllegalArgumentException 的以下异常:@androidx.room.Database 未定义元素视图()