在我的android应用程序中,我可以很好地访问SQLite数据库,除了onClick()函数之外,我无法找出原因。在logcat中没有例外也没有消息,我不确定在哪里寻找错误。我几乎可以肯定它是某种类型的上下文错误,但我对如何调试它感到困惑。任何人都可以给我一些提示吗?
我对Android相对缺乏经验,所以请善待,但这里有一些代表我正在做的事情:
public class DBAccessClass {
private SQLiteDatabase mydb;
private Context context;
public DBAccessClass(Context c){
context = c;
}
public void initMyDB(){
mydb = context.openOrCreateDatabase("myapplicationdb", MODE_PRIVATE, null);
}
public Cursor someQuery(){
Cursor rows = mydb.rawQuery("SELECT * FROM myTable", null);
return rows;
}
}
public class FullscreenActivity extends Activity implements View.OnClickListener{
public DBAccessClass dbAccess;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
dbAccess = new DBAccessClass(this);
dbAccess.initMyDB();
}
.....
@Override
public void onClick(View v){
Cursor rows = dbAccess.someQuery(); // <<<------ I get no rows here, no data, no error!!
SQLiteDatabase anotherTry = openOrCreateDatabase("myapplicationdb", MODE_PRIVATE, null);
Cursor otherRows = anotherTry.rawQuery("SELECT * FROM myTable", null); // <<<< No data and no error here either!
}
public void someOtherFunction(){
Cursor rows = dbAccess.someQuery(); // <<<------- I DO get data here!
}
}