android - SQLite crashes when getWritableDatabase() is called on a Background Running Volley Request -


the request able execute getwritabledatabase() when caller fragment running. when fragment closed or app closed, volley request executing in background not able execute getwritabledatabase()

public class contactstablemanager { private context context; private dbhelper ourhelper; private sqlitedatabase ourdatabase; private static final string database_name = "contacts"; private static final string database_table = "contactstable"; private static final int database_version = 1; public static final string tag = "contactsmanager.java"; public static final string key_name = "name"; public static final string key_number = "number"; public static final string key_id = "id";    public contactstablemanager(context c) {     context = c; }  public contactstablemanager open() {       ourhelper = new dbhelper(context);     ourdatabase = ourhelper.getwritabledatabase();     return this; }  public void close() {     ourhelper.close();  } public boolean addentry(string name, string number) {      boolean success=false;     contentvalues cv = new contentvalues();     cv.put(key_name,name);     cv.put(key_number,number);     if(!checkdata(cv)){         open();         success = ourdatabase.insert(database_table, null, cv) > 0;         close();     }      return success; } private static class dbhelper extends sqliteopenhelper {      public dbhelper(context context) {         super(context, database_name, null, database_version);     }      @override     public void oncreate(sqlitedatabase db) {         db.execsql("create table " + database_table + " (" +                         key_id + " integer primary key autoincrement, " +                         key_name + " text not null, " + key_number + " text not null); "         );      }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         db.execsql("drop table if exists " + database_table);         oncreate(db);     } } 

}

volley request code, executed on fragment, tried passing getactivity() in oncreateview(). didn't work.

    private void sendnumberrequest(final string name, final string phonenumber) {       string tag_string_req = "req_contact";        stringrequest strreq = new stringrequest(request.method.post,             appconfig.url_contacts, new response.listener<string>() {          @override         public void onresponse(string response) {             response=response.substring(response.indexof("{"));               try {                 jsonobject jobj = new jsonobject(response);                 log.e(tag, "jsonobject: " + jobj.tostring());                 boolean error = jobj.getboolean("error");                  // check error node in json                 if (!error) {                      jsonobject user = jobj.getjsonobject("user");                      contactstablemanager contactstablemanager1=new contactstablemanager(getactivity());                          // inserting row in users table                      contactstablemanager1.addentry(name,phonenumber);                      makescreen();                  }             } catch (jsonexception e) {                 // json error                 log.e(tag, "login response: catch");                 e.printstacktrace();             }          }     }, new response.errorlistener() {          @override         public void onerrorresponse(volleyerror error) {             log.e(tag, "login error: " + error.getmessage());           }     }) {          @override         protected map<string, string> getparams() {             // posting parameters login url             map<string, string> params = new hashmap<string, string>();             params.put("tag", "contact_req");             params.put("name", name);             params.put("number", phonenumber);              return params;         }      };      // adding request request queue     appcontroller.getinstance().addtorequestqueue(strreq, tag_string_req); } 

when activity closes context used database has ceased exist. if want abort operation can cancel volley requests in possibly ondestroy() using queue.cancelall(tag_string_req);

if want persist data outside scope of application use service.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -