2016-10-05 34 views
1

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

请使用logcat来确定导致崩溃和发生异常的代码行的异常。 –

+0

在这里发布您的Logcat以查看它 –

+0

debug searchpass()并查看查询是什么。不应该有任何非必需空间 –

回答

-1

在此您同时使用cursor.moveToNextcursor.movetoFirst两者。我认为你不应该这样做。使用一个是cursor.moveToNext

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

无论哪种方式都是有效的 – Shmuel

+0

感谢兄弟......它的正确..现在它的工作......非常感谢你...... @sunil – Aadhi

相关问题