gpt4 book ai didi

java - 当我搜索数据时,我的应用程序被迫停止。我该如何解决这个问题?

转载 作者:行者123 更新时间:2023-12-01 06:08:25 26 4
gpt4 key购买 nike

当我在数据库中搜索数据时,我的应用程序被迫停止。只有当行数超过 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;
}

最佳答案

在此,您同时使用了 cursor.moveToNextcursor.movetoFirst 。我认为你不应该两者都做。使用 cursor.moveToNext

    while (cursor.moveToNext()) {
a = cursor.getString(0);
if (a.equals(user_name)) {
b = cursor.getString(1);
}
}

关于java - 当我搜索数据时,我的应用程序被迫停止。我该如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39872462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com