android - SQLite MissingFormatArgumentException -


i'm following sqlite tutorial on android, , stumped.
appreciated! :)

my code create database representing kitchen inventory, adding/deleting items accordingly.
class used:
kitchenitem - representation of kitchen inventory item
kitchendbhelper - extends sqliteopenhelper, create db
kitchendatasource - actual adding/deleting items on db
kitcheninventoryactivity - setting layout , intializing kitchendatasource

but when try run kitcheninventoryactivity, exception:
exception ljava/util/missingformatargumentexception; thrown while initializing lkitchendbhelper;
thais part in oncreate() new kitchendatasource.
want capture more logs, unfortunately can't add logs constructor. :(
believe of code irrelevant, still post codes below.
or suggestions appreciated!

code of kitchendbhelper below:

import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper;  //creates db public class kitchendbhelper extends sqliteopenhelper {      private static final string database_name = "kitchen.db";     private static final int database_version = 1;     public static final string table_kitchen = "kitchen";      //columns in database     public static final string key_id = "id";     public static final string key_category = "category";     public static final string key_item = "item";     public static final string key_amount = "amount";       private static final string database_create_statement =              string.format("create_table %s ( %s integer primary key, %s text,     %s text, %s text;",                            table_kitchen,                           key_id,                           key_category,                           key_amount);      public kitchendbhelper(context context) {         super(context, database_name, null, database_version);     }      @override     public void oncreate(sqlitedatabase database) {         database.execsql(database_create_statement);     }      @override     public void onupgrade(sqlitedatabase database, int oldversion, int newversion) {         database.execsql("drop table if exists " + table_kitchen);         oncreate(database);     }  } 

code of kitchenitem:

import android.util.log;  //this used entries kitchendb public class kitchenitem {     //todo: modified in future     private long id;     private string category;     private string itemname;     private string amount;      public kitchenitem() {      }      public kitchenitem(int id, string category, string itemname, int amount) {         this.id = id;         this.category = category;         this.itemname = itemname;         this.amount = integer.tostring(amount);     }      public kitchenitem(string itemname, int amount) {         this.itemname = itemname;         this.amount = integer.tostring(amount);     }      public long getid() {         return id;     }      public void setid(long id) {         this.id = id;     }      public string getcategory() {         return category;     }      public void setcategory(string category) {         this.category = category;     }      public string getitemname() {         return itemname;     }      public void setitemname(string itemname) {         this.itemname = itemname;     }      public string getamount() {         return amount;     }      public void setamount(int amount) {         this.amount = integer.tostring(amount);     }      @override     public string tostring() {         log.d("kitchen", "tostring in kitchenitem");         return "itemname";     } } 

code of kitchendatasource:

import java.util.arraylist; import java.util.list;  import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase;   public class kitchendatasource {     private sqlitedatabase database;     private kitchendbhelper dbhelper;     private string[] allcolumns = {kitchendbhelper.key_id,                                       kitchendbhelper.key_category,                                kitchendbhelper.key_item,                                kitchendbhelper.key_amount};      public kitchendatasource(context context) {         dbhelper = new kitchendbhelper(context);     }      public void open() throws sqlexception {         database = dbhelper.getwritabledatabase();     }      public void close() {         dbhelper.close();     }      public kitchenitem additem(kitchenitem item) {         contentvalues values = new contentvalues();         values.put(kitchendbhelper.key_category, item.getcategory());         values.put(kitchendbhelper.key_item, item.getitemname());         values.put(kitchendbhelper.key_amount, item.getamount());          long insertid = database.insert(kitchendbhelper.table_kitchen, null, values);          cursor cursor = database.query(kitchendbhelper.table_kitchen,                                         allcolumns,                                     kitchendbhelper.key_id + " = " + insertid,                                     null, null, null, null);          cursor.movetofirst();         kitchenitem newkitchenitem = cursortokitchenitem(cursor);         cursor.close();          return newkitchenitem;     }      public kitchenitem getitem(int id) {         cursor cursor = database.query(kitchendbhelper.table_kitchen,                               new string[] {allcolumns[0], allcolumns[1], allcolumns[2], allcolumns[3]},                              kitchendbhelper.key_id + "=?",                               new string[] {string.valueof(id)},                              null, null, null, null);         if(cursor != null) {             cursor.movetofirst();         }          kitchenitem item = new kitchenitem(integer.parseint(cursor.getstring(0)),                                         cursor.getstring(1),                                         cursor.getstring(2),                                        integer.parseint(cursor.getstring(3)));          cursor.close();          return item;     }      public list<kitchenitem> getallitems() {         list<kitchenitem> itemlist = new arraylist<kitchenitem>();         cursor cursor = database.query(kitchendbhelper.table_kitchen,                                     allcolumns, null, null, null, null, null);          cursor.movetofirst();         while(!cursor.isafterlast()) {             kitchenitem item = cursortokitchenitem(cursor);             itemlist.add(item);             cursor.movetonext();         }         cursor.close();          return itemlist;     }      public int updateitem(kitchenitem item) {                contentvalues values = new contentvalues();         values.put(kitchendbhelper.key_category, item.getcategory());         values.put(kitchendbhelper.key_item, item.getitemname());         values.put(kitchendbhelper.key_amount, item.getamount());          return database.update(kitchendbhelper.table_kitchen, values, kitchendbhelper.key_id + " = ?", new string[] {string.valueof(item.getid())});     }      public void deleteitem(kitchenitem item) {         long id = item.getid();         database.delete(kitchendbhelper.table_kitchen,                      kitchendbhelper.key_id + " = " + id,                     null);     }      private kitchenitem cursortokitchenitem(cursor cursor) {         kitchenitem item = new kitchenitem();         item.setid(cursor.getlong(0));         item.setcategory(cursor.getstring(1));         item.setitemname(cursor.getstring(2));         item.setamount(integer.parseint(cursor.getstring(3)));          return item;     } } 

last not least, kitcheninventoryactivity:

import java.util.list;  import android.app.listactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.arrayadapter;  public class kitcheninventoryactivity extends listactivity {     private kitchendatasource datasource;     @override     public void oncreate(bundle savedinstancestate) {         log.d("kitchen", "kitcheninventoryactivity oncreate");         super.oncreate(savedinstancestate);         setcontentview(r.layout.kitchen_inventory_activity);          datasource = new kitchendatasource(this);         log.d("kitchen", "datasource initialized");         datasource.open();          list<kitchenitem> itemlist = datasource.getallitems();         arrayadapter<kitchenitem> adapter = new arrayadapter<kitchenitem>(this,                                                                        android.r.layout.simple_list_item_1,                                                                        itemlist);         setlistadapter(adapter);     }      public void onclick(view clickedbutton) {         @suppresswarnings("unchecked")         arrayadapter<kitchenitem> adapter = (arrayadapter<kitchenitem>)getlistadapter();         kitchenitem item = null;         switch(clickedbutton.getid()) {         case r.id.add:             //todo: using preconstructed item now, need add user input             kitchenitem tempitem = new kitchenitem("pizza", 5);             item = datasource.additem(tempitem);             adapter.add(item);             break;         case r.id.delete:             //todo: deleting first item now, need add selection             if(getlistadapter().getcount() > 0) {                 item = (kitchenitem)getlistadapter().getitem(0);                 datasource.deleteitem(item);                 adapter.remove(item);             }             break;         }         adapter.notifydatasetchanged();     }      @override     protected void onresume() {         datasource.open();         super.onresume();     }      @override     protected void onpause() {         datasource.close();         super.onpause();     }  } 

your create statement has 5 replacement characters (%s), have 4 variables... assume forgot key_item:

string.format("create table %s (%s integer primary key, %s text, %s text, %s text);",                 table_kitchen,                key_id,                key_category,                key_item,                key_amount); 

also, changed create_table create table , added missing ).
lastly, android requires primary key have name "_id", won't except "id"...


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -