當我搜索數據庫中的數據時,我的應用程序被迫停止。只有當超過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()並查看查詢是什麼。不應該有任何非必需空間 –