android - Error with inserting data into sqlite database -
i'm trying insert data sqlite database using dialog wrapper. however, unable so. have 2 texts called title , template insert database. crashes. below java code:
@override public boolean oncreateoptionsmenu(menu menu) { menu.add(0,menu_item_insert,0,r.string.menu_insert); return true; }
@override public boolean onoptionsitemselected(menuitem item) { switch(item.getitemid()) { case menu_item_insert: insert(); //startactivity(new intent(intent.action_insert,getintent().getdata())); return true; } return(super.onoptionsitemselected(item)); } @override public void oncreatecontextmenu(contextmenu menu,view v,contextmenu.contextmenuinfo menuinfo) { adapterview.adaptercontextmenuinfo info; try { info = (adapterview.adaptercontextmenuinfo) menuinfo; } catch (classcastexception e) { log.e(tag, "bad menuinfo", e); return; } mcursor = (cursor) getlistadapter().getitem(info.position); menu.setheadertitle(mcursor.getstring(column_index_title)); menu.add(0,menu_item_delete,0 ,r.string.menu_delete); menu.add(1,menu_item_addtmessage,1,r.string.menu_add); } @override public boolean oncontextitemselected(menuitem item) { adapterview.adaptercontextmenuinfo info; try { info = (adapterview.adaptercontextmenuinfo) item.getmenuinfo(); } catch (classcastexception e) { log.e(tag, "bad menuinfo", e); return false; } switch(item.getitemid()) { case menu_item_delete: //delete selected row delete(info.id); return true; case menu_item_addtmessage: //add message return true; } return(super.oncontextitemselected(item)); } private void insert() { layoutinflater inflater = layoutinflater.from(this); view addview = inflater.inflate(r.layout.templates_editor,null); final dialogwrapper wrapper = new dialogwrapper(addview); new alertdialog.builder(this) .settitle(r.string.menu_insert) .setview(addview) .setpositivebutton(r.string.item_ok,new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { processinsert(wrapper); } }) .setnegativebutton(r.string.item_cancel,new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { // ignore, dismiss } }) .show(); } private void delete(final long rowid) { if(rowid > 0) { new alertdialog.builder(this) .settitle(r.string.menu_delete) .setpositivebutton(r.string.item_ok, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { processdelete(rowid); } }) .setnegativebutton(r.string.item_cancel, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { //dismiss dialog } }).show(); } } private void processdelete(long rowid) { string[] args = {string.valueof(rowid)}; db.getwritabledatabase().delete("templates", templates._id, args); mcursor.requery(); } private void processinsert(dialogwrapper wrapper) { contentvalues cv = new contentvalues(); cv.put("title", wrapper.gettitle()); cv.put("template", wrapper.gettemplate()); db.getwritabledatabase().insert("templates", "title", cv); db.getwritabledatabase().insert("templates", "template", cv); mcursor.requery(); } class dialogwrapper{ edittext titlefield = null; edittext templatefield = null; view base = null; dialogwrapper(view base) { this.base = base; } string gettitle(){ return(gettitlefield().gettext().tostring()); } string gettemplate(){ return(gettemplatefield().gettext().tostring()); } private edittext gettitlefield(){ if(titlefield == null){ titlefield = (edittext) findviewbyid(r.id.title); } return titlefield; } private edittext gettemplatefield(){ if(templatefield == null){ templatefield = (edittext) findviewbyid(r.id.template); } return templatefield; } }
below logcat:
12-02 08:23:17.712: error/androidruntime(204): uncaught handler: thread main exiting due uncaught exception
12-02 08:23:17.862: error/androidruntime(204): java.lang.nullpointerexception
12-02 08:23:17.862: error/androidruntime(204): @ joel.groupsms.templateslist$dialogwrapper.gettitle(templateslist.java:214)
12-02 08:23:17.862: error/androidruntime(204): @ joel.groupsms.templateslist.processinsert(templateslist.java:196)
12-02 08:23:17.862: error/androidruntime(204): @ joel.groupsms.templateslist.access$0(templateslist.java:192)
12-02 08:23:17.862: error/androidruntime(204): @ joel.groupsms.templateslist$1.onclick(templateslist.java:150)
12-02 08:23:17.862: error/androidruntime(204): @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:158)
12-02 08:23:17.862: error/androidruntime(204): @ android.os.handler.dispatchmessage(handler.java:99)
12-02 08:23:17.862: error/androidruntime(204): @ android.os.looper.loop(looper.java:123)
12-02 08:23:17.862: error/androidruntime(204): @ android.app.activitythread.main(activitythread.java:4363)
12-02 08:23:17.862: error/androidruntime(204): @ java.lang.reflect.method.invokenative(native method)
12-02 08:23:17.862: error/androidruntime(204): @ java.lang.reflect.method.invoke(method.java:521)
12-02 08:23:17.862: error/androidruntime(204): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:860)
12-02 08:23:17.862: error/androidruntime(204): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:618)
12-02 08:23:17.862: error/androidruntime(204): @ dalvik.system.nativestart.main(native method)
edit
stacktrace requested pertaining comment:
12-06 16:54:12.466: info/activitymanager(58): starting activity: intent { cmp=joel.groupsms/.templateedit }
12-06 16:54:16.550: debug/androidruntime(254): shutting down vm 12-06 16:54:16.550: warn/dalvikvm(254): threadid=3: thread exiting uncaught exception (group=0x4001b188)
12-06 16:54:16.550: error/androidruntime(254): uncaught handler: thread main exiting due uncaught exception
12-06 16:54:16.736: error/androidruntime(254): java.lang.runtimeexception: unable start activity componentinfo{joel.groupsms/joel.groupsms.templateedit}: java.lang.nullpointerexception
12-06 16:54:16.736: error/androidruntime(254): @ android.app.activitythread.performlaunchactivity(activitythread.java:2496)
12-06 16:54:16.736: error/androidruntime(254): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2512)
12-06 16:54:16.736: error/androidruntime(254): @ android.app.activitythread.access$2200(activitythread.java:119)
12-06 16:54:16.736: error/androidruntime(254): @ android.app.activitythread$h.handlemessage(activitythread.java:1863)
12-06 16:54:16.736: error/androidruntime(254): @ android.os.handler.dispatchmessage(handler.java:99)
12-06 16:54:16.736: error/androidruntime(254): @ android.os.looper.loop(looper.java:123)
12-06 16:54:16.736: error/androidruntime(254): @ android.app.activitythread.main(activitythread.java:4363)
12-06 16:54:16.736: error/androidruntime(254): @ java.lang.reflect.method.invokenative(native method)
12-06 16:54:16.736: error/androidruntime(254): @ java.lang.reflect.method.invoke(method.java:521)
12-06 16:54:16.736: error/androidruntime(254): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:860)
12-06 16:54:16.736: error/androidruntime(254): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:618)
12-06 16:54:16.736: error/androidruntime(254): @ dalvik.system.nativestart.main(native method)
12-06 16:54:16.736: error/androidruntime(254): caused by: java.lang.nullpointerexception
12-06 16:54:16.736: error/androidruntime(254): @ android.content.contentresolver.acquireprovider(contentresolver.java:754)
12-06 16:54:16.736: error/androidruntime(254): @ android.content.contentresolver.query(contentresolver.java:197)
12-06 16:54:16.736: error/androidruntime(254): @ android.app.activity.managedquery(activity.java:1495)
12-06 16:54:16.736: error/androidruntime(254): @ joel.groupsms.templateedit.oncreate(templateedit.java:77)
12-06 16:54:16.736: error/androidruntime(254): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047)
12-06 16:54:16.736: error/androidruntime(254): @ android.app.activitythread.performlaunchactivity(activitythread.java:2459)
it's hard tell without line numbers in code match traceback i'm guessing (edittext) findviewbyid(r.id.title)
returning null. check app in hierarchyviewer @ time you're doing insert , make sure view think there in hierarchy there @ time you're processing insert code.
Comments
Post a Comment