我有这个静态方法,可以从数据库(SQLite)中检索我登录的用户信息,并且有时出于未知原因,该方法返回null ...不幸的是,我无法重现此错误...
private static final Object sLock = new Object() {};
public static User getUser() {
User user = null;
synchronized (sLock) {
Cursor cursor = getContext().getContentResolver().query(
MyContract.User.CONTENT_URI, null, null, null, null);
TypeAdapter<User> adapter = GsonUtils.createGsonBuilder().getAdapter(User.class);
try {
if (cursor != null) {
if (cursor.moveToFirst()) {
user = adapter.fromJson(cursor.getString(1));
}
}
} catch (IOException e) {
LogUtils.LOGE(TAG, "Impossible to parse from Gson logged user");
} finally {
if (cursor != null) {
cursor.close();
}
}
if (user == null) {
LogUtils.LOGE(TAG, "Impossible to retrieve logged user from database");
}
}
return user;
}
你们有一些解释吗?
非常感谢您!