由于此查询,我的应用程序崩溃了。我该怎么做才能知道里面的问题是什么?这对我来说似乎是正确的
public User getUser(String userName) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
cursor = db.query("LOGIN", new String[] { "USERNAME",
"PASSWORD", "STATUS" }, "USERNAME" + "=?",
new String[] { String.valueOf(userName) }, null, null, null, null);
if (cursor != null)
{
cursor.moveToFirst();
}
User user = new User();
user.setName(cursor.getString(cursor.getColumnIndex("USERNAME")));
user.setPassword(cursor.getString(cursor.getColumnIndex("PASSWORD")));
user.setstatus(cursor.getString(cursor.getColumnIndex("STATUS")));
return user;
}
答案 0 :(得分:1)
可能你的光标正在返回一个空/空游标......现在你已经检查了它
if (cursor != null)
{
cursor.moveToFirst();
}
但在此之后,您直接尝试从光标设置用户数据而不检查它是否为空,请尝试改为:
User user=null;
if (cursor != null)
{
user= new User();
cursor.moveToFirst();
user.setName(cursor.getString(cursor.getColumnIndex("USERNAME")));
user.setPassword(cursor.getString(cursor.getColumnIndex("PASSWORD")));
user.setstatus(cursor.getString(cursor.getColumnIndex("STATUS")));
}
if(user==null){
// your cursor returned null... check your query/db
}
//this will return a null object if your cursor returns null... you can handle it later
return user;
答案 1 :(得分:0)
你可以使用SQliteManger检查查询或打开以下链接并写下你的查询然后你会发现它的正确与否