当我搜索数据库中的数据时,我的应用程序被迫停止。只有当超过2行时才会发生,否则就没有问题。我可以很好地插入数据。我该如何解决这个问题?当我搜索数据时,我的应用程序被迫停止。我该如何解决这个问题?
//click event
public void OnButtonClick(View v) {
dbhelper.open();
EditText a = (EditText)findViewById(R.id.user_tf);
String User_Name = a.getText().toString();
EditText b = (EditText)findViewById(R.id.password_tf);
String Password = b.getText().toString();
sqlitedatabase = dbhelper.getReadableDatabase();
String password = dbhelper.searchpass(User_Name);
if(Password.equals(password)) {
Toast.makeText(getBaseContext(), "User and pass correct", Toast.LENGTH_LONG).show();
dbhelper.close();
Intent intent = new Intent(Login_activity.this, Homepage.class);
startActivity(intent);
} else {
Toast.makeText(getBaseContext(),"User or pass incorrect", Toast.LENGTH_LONG).show();
dbhelper.close();
Intent intent = new Intent(Login_activity.this, Login_activity.class);
startActivity(intent);
}
// Searchpass in DBHelper class
public String searchpass(String user_name) {
db.isOpen();
db = this.getReadableDatabase();
String query = "SELECT " + UserConstruct.newUserinfo.UserName + ", " + UserConstruct.newUserinfo.Password + " FROM " + UserConstruct.newUserinfo.TableName + " ";
// int a = query.length();
Cursor cursor = db.rawQuery(query, null);
String a, b;
b = "not found";
do {
if (cursor.moveToFirst()) {
a = cursor.getString(0);
if (a.equals(user_name)) {
b = cursor.getString(1);
}
}
} while (cursor.moveToNext());
return b;
}
请使用logcat来确定导致崩溃和发生异常的代码行的异常。 –
在这里发布您的Logcat以查看它 –
debug searchpass()并查看查询是什么。不应该有任何非必需空间 –