我通过在SQLiteDatabase
中输入密钥来访问EditText
中的数据。每当我输入一个错误的密钥时,我的应用程序就停止了。我不希望我的申请停止,而是我想在这里展示吐司。
我该怎么做?
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 :(得分:1)
在使用光标之前,请务必检查光标计数
if (cursor != null && cursor.getCount() > 0) {
// your logic goes here
} else {
Toast.makeText(getApplicationContext(), "No Record Found", 1000).show();
}
答案 1 :(得分:0)
我认为您应该使用c == null
代替c.equals(null)
,因为
来自doc:
equals方法在非null上实现等价关系 对象引用
...
对于任何非空引用值x和y,此方法返回true,如果 并且只有当x和y引用同一个对象时(x == y才有值) 真)。