android - how to call getWritableDatabase() from a different thread or intent service, App crashing on calling it in the main thread -


this main activity

import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.edittext; import android.widget.textview;    public class mainactivity extends appcompatactivity {      edittext myinput;     textview mytext;     mydbhandler dbhandler;         @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);             myinput =(edittext) findviewbyid(r.id.myinput);         mytext =(textview) findviewbyid(r.id.mytext);         dbhandler= new mydbhandler(this, null, null, 1);         printdatabase();       }      //add product database      public void addstuff(view view)     {         products product = new products(myinput.gettext().tostring());         dbhandler.addproduct(product);         printdatabase();      }      //delete items database      public void deletestuff(view view)     {         string inputtext= myinput.gettext().tostring();         dbhandler.deleteproduct(inputtext);         printdatabase();       }      public void printdatabase()     {         string dbstring= dbhandler.databasetostring();         mytext.settext(dbstring);         myinput.settext("");     }  } 

the products class, , set products

public class products {      private int _id;     private string _productname;      public products()     {      }      public products(string productname) {         this._productname = productname;     }      public void set_id(int _id) {         this._id = _id;     }      public void set_productname(string _productname) {         this._productname = _productname;     }      public int get_id() {         return _id;     }      public string get_productname() {         return _productname;     } } 

the database handler class,

import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.database.cursor; import android.content.context; import android.content.contentvalues;   public class mydbhandler extends sqliteopenhelper{      private static final int database_version = 1;     private static final string database_name = "products.db";     public static final string table_products = "products";     public static final string column_id ="_id";     public static final string column_productname ="productname";       public mydbhandler(context context, string name, sqlitedatabase.cursorfactory factory, int version) {         super(context, database_name, factory, database_version);     }       @override     public void oncreate(sqlitedatabase db) {          string query= "create table " + table_products + "(" +                 column_id + " integer primary key autoincrement , " +                 column_productname + " text " +                 ");";         db.execsql(query);      }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         db.execsql("drop table if exists "+table_products);         oncreate(db);     }      //add new row database     public void addproduct(products product)     {         contentvalues values = new contentvalues();         values.put(column_productname, product.get_productname());         sqlitedatabase db= getwritabledatabase();         db.insert(table_products, null, values);         db.close();     }      //delete product database     public void deleteproduct(string productname)     {         sqlitedatabase db= getwritabledatabase();         db.execsql("delete "+ table_products + " " + column_productname + " =\"" + productname + "\";" );     }      //printing out database string      public string databasetostring()     {         string dbstring ="";         sqlitedatabase db= getwritabledatabase();         string query = "select * "+ table_products + " 1";          //cursor points location in database results         cursor c= db.rawquery(query,null);         //go 1st row in results         c.movetofirst();          while(!c.isafterlast())         {             if(c.getstring(c.getcolumnindex("productname"))!=null)             {                 dbstring += c.getstring(c.getcolumnindex("productname"));                 dbstring += "\n";              }          }         db.close();         return dbstring;      }    } 

i tried putting getwritabledatabase() codes in runnables, coud'nt databasetostring() method.

how call getwritabledatabase() intent service??

i want use intent service getwritabledatabase()

import android.app.intentservice; import android.content.intent;  public class myintent extends intentservice{      public myintent(string name) {         super("myintent");     }      @override     protected void onhandleintent(intent intent) {      } } 

i don't know how through intent service

this logcat after app starts

8-19 17:56:08.848  15523-15523/? i/art﹕ late-enabling -xcheck:jni 08-19 17:56:08.848  15523-15523/? i/art﹕ vmhook: rlim_cur : 0 pid:15523 08-19 17:56:09.218  15523-15523/com.example.vashisht.sqliteapp d/atlas﹕ validating map... 08-19 17:56:09.278  15523-15554/com.example.vashisht.sqliteapp i/adreno-egl﹕ <qegldrvapi_eglinitialize:410>: egl 1.4 qualcomm build: test sba la.bf.1.1.1_rb1 au_linux_android_la.bf.1.1.1_rb1.05.00.02.042.012 + c1105519 + c_apilogging ()     opengl es shader compiler version: e031.25.03.00     build date: 01/23/15 fri     local branch:     remote branch: refs/tags/au_linux_android_la.bf.1.1.1_rb1.05.00.02.042.012     local patches: none     reconstruct branch: nothing 08-19 17:56:09.478  15523-15523/com.example.vashisht.sqliteapp i/inputmethodmanager﹕ [startinputinner] editorinfo { packagename=com.example.vashisht.sqliteapp, inputtype=0x20001, imeoptions=0x40000006, privateimeoptions=null }, windowgainingfocus=android.view.viewrootimpl$w@1287b4e7, mservedview=android.support.v7.widget.appcompatedittext{e909894 vfed..cl .f....i. 90,222-990,335 #7f0c004f app:id/myinput}, mservedinputconnectionwrapper=android.view.inputmethod.inputmethodmanager$controlledinputconnectionwrapper@18863f3d 

when enter text in textfield , tap addbutton(to add entries datbase table) app freezes , logcat goes this:

8-19 17:56:09.478  15523-15523/com.example.vashisht.sqliteapp i/inputmethodmanager﹕ [startinputinner] editorinfo { packagename=com.example.vashisht.sqliteapp, inputtype=0x20001, imeoptions=0x40000006, privateimeoptions=null }, windowgainingfocus=android.view.viewrootimpl$w@1287b4e7, mservedview=android.support.v7.widget.appcompatedittext{e909894 vfed..cl .f....i. 90,222-990,335 #7f0c004f app:id/myinput}, mservedinputconnectionwrapper=android.view.inputmethod.inputmethodmanager$controlledinputconnectionwrapper@18863f3d 08-19 17:57:30.878  15523-15523/com.example.vashisht.sqliteapp i/inputmethodmanager﹕ [startinputinner] editorinfo { packagename=com.example.vashisht.sqliteapp, inputtype=0x20001, imeoptions=0x40000006, privateimeoptions=null }, windowgainingfocus=null, mservedview=android.support.v7.widget.appcompatedittext{e909894 vfed..cl .f...... 90,222-990,335 #7f0c004f app:id/myinput}, mservedinputconnectionwrapper=android.view.inputmethod.inputmethodmanager$controlledinputconnectionwrapper@2072768a 08-19 17:57:30.888  15523-15523/com.example.vashisht.sqliteapp w/iinputconnectionwrapper﹕ performprivatecommand on inactive inputconnection 08-19 17:57:30.918  15523-15523/com.example.vashisht.sqliteapp w/iinputconnectionwrapper﹕ performprivatecommand on inactive inputconnection 08-19 17:57:35.338  15523-15523/com.example.vashisht.sqliteapp i/inputmethodmanager﹕ [startinputinner] editorinfo { packagename=com.example.vashisht.sqliteapp, inputtype=0x20001, imeoptions=0x40000006, privateimeoptions=null }, windowgainingfocus=android.view.viewrootimpl$w@1287b4e7, mservedview=android.support.v7.widget.appcompatedittext{e909894 vfed..cl .f....i. 90,222-990,335 #7f0c004f app:id/myinput}, mservedinputconnectionwrapper=android.view.inputmethod.inputmethodmanager$controlledinputconnectionwrapper@2996b4fb 08-19 17:57:42.158  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 5.091ms 08-19 17:57:42.608  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 9.910ms 08-19 17:57:55.408  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background partial concurrent mark sweep gc freed 180(6kb) allocspace objects, 108(15mb) los objects, 43% free, 5mb/9mb, paused 9.448ms total 36.167ms 08-19 17:57:59.468  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background partial concurrent mark sweep gc freed 43(1488b) allocspace objects, 24(4mb) los objects, 40% free, 6mb/10mb, paused 10.028ms total 18.813ms 08-19 17:58:00.478  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 9.383ms 08-19 17:58:11.218  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 5.913ms 08-19 17:58:14.328  15523-15537/com.example.vashisht.sqliteapp i/art﹕ waitforgctocomplete blocked 5.482ms cause heaptrim 08-19 17:58:15.628  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background partial concurrent mark sweep gc freed 60(2064b) allocspace objects, 32(7mb) los objects, 39% free, 6mb/10mb, paused 11.181ms total 21.280ms 08-19 17:58:16.248  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 10.318ms 08-19 17:58:26.088  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background sticky concurrent mark sweep gc freed 19(688b) allocspace objects, 9(2mb) los objects, 24% free, 5mb/7mb, paused 5.279ms total 10.035ms 08-19 17:58:28.778  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 5.467ms 08-19 17:58:31.278  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 6.368ms 08-19 17:58:34.298  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 6.308ms 08-19 17:58:34.348  15523-15537/com.example.vashisht.sqliteapp i/art﹕ waitforgctocomplete blocked 10.169ms cause heaptrim 08-19 17:58:36.798  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 5.915ms 08-19 17:58:38.918  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 16.815ms 08-19 17:58:44.538  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 8.032ms 08-19 17:58:44.878  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 5.364ms 08-19 17:58:44.878  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background partial concurrent mark sweep gc freed 35(1184b) allocspace objects, 22(7mb) los objects, 54% free, 3mb/7mb, paused 5.855ms total 17.791ms 08-19 17:58:46.248  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 7.053ms 08-19 17:58:46.268  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 9.842ms 08-19 17:58:46.268  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background sticky concurrent mark sweep gc freed 26(928b) allocspace objects, 12(4mb) los objects, 21% free, 6mb/7mb, paused 10.751ms total 18.381ms 08-19 17:58:48.828  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 5.178ms 08-19 17:58:48.868  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 7.268ms 08-19 17:58:49.538  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 9.654ms 08-19 17:58:50.838  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 7.434ms 08-19 17:58:54.758  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 8.813ms 08-19 17:58:54.758  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background sticky concurrent mark sweep gc freed 24(864b) allocspace objects, 11(3mb) los objects, 27% free, 6mb/8mb, paused 9.415ms total 14.852ms 08-19 17:58:56.798  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 9.198ms 08-19 17:58:56.808  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background partial concurrent mark sweep gc freed 39(1328b) allocspace objects, 23(8mb) los objects, 39% free, 6mb/10mb, paused 10.067ms total 19.829ms 08-19 17:59:03.098  15523-15538/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 7.042ms 08-19 17:59:03.108  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background partial concurrent mark sweep gc freed 23(784b) allocspace objects, 14(5mb) los objects, 54% free, 3mb/7mb, paused 7.720ms total 22.105ms 08-19 17:59:04.368  15523-15537/com.example.vashisht.sqliteapp i/art﹕ waitforgctocomplete blocked 8.374ms cause heaptrim 08-19 17:59:18.878  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 8.496ms 08-19 17:59:21.378  15523-15533/com.example.vashisht.sqliteapp w/art﹕ suspending threads took: 6.302ms 08-19 17:59:21.658  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background partial concurrent mark sweep gc freed 35(1184b) allocspace objects, 21(8mb) los objects, 39% free, 6mb/11mb, paused 5.077ms total 23.084ms 08-19 17:59:24.078  15523-15538/com.example.vashisht.sqliteapp i/art﹕ background partial concurrent mark sweep gc freed 27(928b) allocspace objects, 15(6mb) los objects, 52% free, 3mb/7mb, paused 9.533ms total 23.103ms 

and manually need kill app, or after time says not responding!!

i modified databasetostring() method return string array can display results in listview.

 public string[] databasetostring()     {           string[] dbstring;         dbstring = new string[]{};          sqlitedatabase db= getreadabledatabase();         string query = "select * "+ table_products + " 1";          //cursor points location in database results         cursor c= db.rawquery(query,null);         //go 1st row in results         c.movetofirst();         int i=0;         while(!c.isafterlast())         {             if(c.getstring(c.getcolumnindex(column_productname))!=null)                 dbstring[i] = c.getstring(c.getcolumnindex(column_productname));              i++;          }         db.close();          return dbstring;       } 

but here , dbstring[i] = c.getstring(c.getcolumnindex(column_productname)); throwing array out of bounds exception???

to call intentservice call

sqlitedatabase = getapplicationcontext().getwritabledatabase(); 

you need getapplicationcontext() since intentservice extends context


Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -