2016-10-05 31 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

相關問題