Reputation: 1632
I am trying to make a database connection with my app. It has an allow and deny button for a visitor. Whenever I try to call an operation of the database class my app force closes. For example, When I click on the Allow button I want the app to store the visitor id, date and string response in the database. Following is the code in the mainActivity that I have for the button:
//databaseVisitor VisDB = new databaseVisitor (this);
//VisitorDatabase VDB = new VisitorDatabase();
//Defining the actions of the Allow Button
Button.OnClickListener allowListener = new Button.OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast allowToast = Toast.makeText(MainActivity.this, R.string.toastYes, Toast.LENGTH_LONG);
allowToast.show();
//Log.d("Insert Log: ", "Inserting add log..");
//count = VisDB.getVisitorCount();
response = "Access Granted";
//dt = date.getDate();
//VisDB.addVisitor(new VisitorDatabase(count+1, dt, response));
}
};
Upvotes: 0
Views: 127
Reputation: 14164
its most probably because you are closing the cursor
cursor.close()
and then making operations on it
return cursor.getCount()
try smthg like this
int count = cursor.getCount();
cursor.close();
return count;
EDIT :
and as for VisDB do it like that
...
databaseVisitor VisDB;
...
public void onCreate(Bundle savedInstanceState) {
...
VisDB = new databaseVisitor (this);
...
}
Upvotes: 2
Reputation: 2759
You closed the cursor before giving getCount (). Try this:
int count = cursor.getCount();
cursor.close();
return count;
Good luck!
Upvotes: 1