Java 当我搜索数据时,我的应用程序被迫停止。我怎样才能解决这个问题?

Java 当我搜索数据时,我的应用程序被迫停止。我怎样才能解决这个问题?,java,android,database,android-cursor,android-database,Java,Android,Database,Android Cursor,Android Database,当我在数据库中搜索数据时,我的应用程序被迫停止。只有当有超过2行时才会发生,否则就可以了。我能很好地插入数据。我怎样才能解决这个问题 //click event public void OnButtonClick(View v) { dbhelper.open(); EditText a = (EditText)findViewById(R.id.user_tf); String User_Name = a.getText().toString(); EditT

当我在数据库中搜索数据时,我的应用程序被迫停止。只有当有超过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.moveToNext
cursor.movetoFirst
。我认为你不应该两者兼而有之。使用一个是
游标。moveToNext

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

请使用logcat确定导致崩溃的异常以及发生异常的代码行。在此处发布您的logcat以查看itdebug searchpass()并查看查询内容。java.io.FileNotFoundException上不应有任何非必需的spacewriteStringToFile错误:/sys/kernel/debug/tracing/tracing\u:/sys/kernel/debug/tracing/tracing\u on:open failed:enoint(无此类文件或目录)writeStringToFile错误:/sys/kernel/debug/binder/transaction_log_enable java.io.FileNotFoundException:/sys/kernel/debug/binder/transaction_log_enable:open failed:enoint(没有这样的文件或目录)这些错误显示在android监视器中..任何一种方式都有效Hanks bro。。。是的。。现在它的工作。。。非常感谢你@苏尼尔