2013-07-15 54 views
0

我通過在EditText中輸入密鑰來訪問SQLiteDatabase中的數據。每當我輸入一些錯誤的密鑰時,我的應用程序就不會停止。我不希望我的申請停止,而是我想在這裏表示敬酒。
我該怎麼做?如何處理Sqlite CursorIndexOutOfBound異常

try{ 

    Log.e("table", table_name); 
    mDb = mDbHelper.getReadableDatabase(); 

    Cursor c = mDb.rawQuery("SELECT name,address1,address2,address3,tariff,sup_type,load,load_unit,meter_no,pole_cd,mst FROM "+table_name+" WHERE acc_id = '"+ i +" ' ", null); 

     if (c.equals(null)) 
     { 
      c.moveToFirst(); 
      Log.e("c", "i am null"+ c.toString()); 
     } 
     else if (c!=null) { 
      c.moveToNext(); 
      Log.e("c", "i am notnull" + c.toString()); 
     } 

     return c; 

    } 
    catch (SQLException mSQLException) 
    { 
     Log.e(TAG, "getTestData >>"+ mSQLException.toString()); 
     throw mSQLException; 
    } 
+0

安置自己的堆棧跟蹤,倒數..更改,如果條件如果(C!= NULL && c.moveToFirst()){...} – Tarun

+0

你嘗試過'c == null'而不是'c.equals(null)'嗎? –

+0

當遊標爲空時,您不能執行'c.moveToFirst();'。所以只檢查'如果(c.moveToFirst()){//空}其他{//不是null}'大家的幫助 –

回答

0

我認爲,你應該使用c == null代替c.equals(null)因爲

從DOC:

的equals方法實現非空 對象的等價關係引用

...

對於任何非空引用值x和y,如果 且僅當x和y引用同一對象(x == y的值爲 爲true),則此方法返回true。

+0

感謝您的幫助:-) –

1

經常檢查光標使用光標一樣

  if (cursor != null && cursor.getCount() > 0) { 
       // your logic goes here 
      } else { 
       Toast.makeText(getApplicationContext(), "No Record Found", 1000).show(); 
      } 
+0

謝謝尋求幫助:-) –