无法从存储库 class 调用(无法解析)来自 SQLiteOpenHelper class 的同步 getinstance 方法

Synchronized getinstance method from SQLiteOpenHelper class can't be called (can't resolved) from repository class

我想使用 Singleton 来实例化 SQLiteOpenHelper,例如 here

我试过将此代码放入我的 SQLiteOpenHelper class:

public static synchronized DatabaseHelper getInstance(Context context) {
    if (sInstance == null) {
        sInstance = new DatabaseHelper(context.getApplicationContext());
    }
    return sInstance;
}

但是当我尝试从我的存储库 class 中调用该方法时,如下所示:

public UserAccountRepository(Context context) {
    super(context);
    dbHelper = new DatabaseHelper.getInstance(context);
}

android工作室显示错误:

cannot resolve symbol getinstance

new用于创建一个新的对象,并调用它的构造函数。

这不是您真正想要做的(getInstance 已经做到了);您只想将 getInstace 的 return 值分配给某个局部变量。 只需删除 new.