我目前在使用android studio时遇到问题,因为我的checkLogIn方法似乎根本不起作用。我想知道是否有人可以指出任何明显的缺陷?
编辑:当我在主活动中调用该方法时,仍然会收到错误消息。在logcat上它显示“getSlotFromBufferLocked:unknown buffer”
//Class manages all data pertaining to the login page
public class DBHandler扩展了SQLiteOpenHelper {
//Declare constants
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "logindetails.db";
private static final String TABLE_LOGIN = "Logindetails";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_LOGIN = "_logIn";
private static final String COLUMN_PASS = "_password";
//Constructor
public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
//On create method - passes through a database object
public void onCreate(SQLiteDatabase db){
//Declares column and table names and data types
String query = "CREATE TABLE " + TABLE_LOGIN + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_LOGIN + " TEXT," +
COLUMN_PASS + " TEXT" +
");";
db.execSQL(query);
}
//On upgrade method
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
onCreate(db);
}
//Adds login details to database - Takes a login object as a parameter.
public void addLogIn(LogIns log){
//Declare values and select columns to pass values through
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_LOGIN, log.get_logIn());
values.put(COLUMN_PASS, log.get_password());
//Insert values into the table
db.insert(TABLE_LOGIN, null, values);
db.close();
}
//reads database and gets list of logins - takes the username and password as an argument
public boolean checkLogIn(String user, String pass){
//Declare and initialise variables
boolean success = false;
SQLiteDatabase db = this.getReadableDatabase();
//select columns in a string array
String[] columns = {COLUMN_LOGIN, COLUMN_PASS};
//execute query
Cursor c = db.query(TABLE_LOGIN, columns, null, null, null, null, null);
//Temp string to store results from query
String rName = "";
String rPass = "";
//loop through all rows and data
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
int iName = c.getColumnIndex(COLUMN_LOGIN);
int iPass = c.getColumnIndex(COLUMN_PASS);
rName = c.getString(iName);
rPass = c.getString(iPass);
if (user.equals(rName) && pass.equals(rPass)){
success = true;
break;
}
}
return success;
}
}
答案 0 :(得分:1)
您的字符串比较不正确。
(rName.equals(user) && rPass.equals(pass))
这可以比较引用而不是值:
private void InitializeDctionary(int typeId, IList < Game > gameInformations, IList < UserInfo > userInformations)
{
if (!_scoreDictionary.ContainsKey(typeId))
{
lock(_scoreDictionaryLock)
{
if (!_scoreDictionary.ContainsKey(typeId))
{
_scoreDictionary.Add(typeId, new Dictionary < int, Dictionary < string, int >> ));
}
foreach(var gameInformation in gameInformations)
{
var orderdUsers = userInformations.Where(s => s.GameId == gameInformation.Id)
.OrderByDescending(s => s.Score)
.Select((value, index) =>
new { value, index //this is the user rank
})
.Select(p =>new {p.value.UserName,p.index})
.Select(p => new KeyValuePair < string, int > (p.UserName, p.index));
if (!_scoreDictionary[typeId].ContainsKey(gameInformation.Id))
_scoreDictionary[typeId].Add(gameInformation.Id, new Dictionary < string, int > ());
_scoreDictionary[typeId][gameInformation.Id] = orderdUsers.ToDictionary(x => x.Key, x => x.Value);
}
}
}
}
这会比较实际的字符串值。
进一步阅读:
How do I compare strings in Java? What is a NullPointerException, and how do I fix it?
如果您有任何问题,请在评论中告诉我。