【问题标题】:Synchronized getinstance method from SQLiteOpenHelper class can't be called (can't resolved) from repository class无法从存储库类调用(无法解析)来自 SQLiteOpenHelper 类的同步 getinstance 方法
【发布时间】:2015-11-11 16:02:47
【问题描述】:

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

我已尝试将此代码放入我的 SQLiteOpenHelper 类中:

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

但是当我尝试像这样从我的存储库类中调用该方法时:

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

android studio 显示错误:

无法解析符号获取实例

【问题讨论】:

    标签: android sqlite instances


    【解决方案1】:

    new 用于创建一个新对象并将其称为构造函数。

    这不是你真正想做的(getInstance 已经完成了);您只想将getInstace 的返回值分配给某个局部变量。 放弃new

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      相关资源
      最近更新 更多