java - Saving SSID Data in SQLite Database Causes Force Close -
i'm attempting update database additional data (the users current ssid) when attempting save database - entire application force closes.
i understand there nullpointer exception being caused when saving data - have no idea i've done wrong cause it.
logcat:
04-04 21:24:08.351: e/androidruntime(10745): fatal exception: asynctask #2 04-04 21:24:08.351: e/androidruntime(10745): java.lang.runtimeexception: error occured while executing doinbackground() 04-04 21:24:08.351: e/androidruntime(10745): @ android.os.asynctask$3.done(asynctask.java:299) 04-04 21:24:08.351: e/androidruntime(10745): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 04-04 21:24:08.351: e/androidruntime(10745): @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 04-04 21:24:08.351: e/androidruntime(10745): @ java.util.concurrent.futuretask.run(futuretask.java:239) 04-04 21:24:08.351: e/androidruntime(10745): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 04-04 21:24:08.351: e/androidruntime(10745): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 04-04 21:24:08.351: e/androidruntime(10745): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 04-04 21:24:08.351: e/androidruntime(10745): @ java.lang.thread.run(thread.java:856) 04-04 21:24:08.351: e/androidruntime(10745): caused by: java.lang.nullpointerexception 04-04 21:24:08.351: e/androidruntime(10745): @ com.app.gamedemo.addeditdevice.savecontact(addeditdevice.java:117) 04-04 21:24:08.351: e/androidruntime(10745): @ com.app.gamedemo.addeditdevice.access$1(addeditdevice.java:111) 04-04 21:24:08.351: e/androidruntime(10745): @ com.app.gamedemo.addeditdevice$1$1.doinbackground(addeditdevice.java:83) 04-04 21:24:08.351: e/androidruntime(10745): @ android.os.asynctask$2.call(asynctask.java:287) 04-04 21:24:08.351: e/androidruntime(10745): @ java.util.concurrent.futuretask.run(futuretask.java:234) 04-04 21:24:08.351: e/androidruntime(10745): ... 4 more
source: (updated w scf's suggestion)
addeditdevice.java
import android.app.activity; import android.app.alertdialog; import android.content.context; import android.net.wifi.wifiinfo; import android.net.wifi.wifimanager; import android.os.asynctask; import android.os.bundle; import android.view.viewgroup; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.framelayout; import android.widget.textview; import android.widget.timepicker; import java.text.decimalformat; public class addeditdevice extends activity { private long rowid; private edittext nameet; private edittext capet; private edittext codeet; private timepicker timeet; private textview ssidtextview; // @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.add_country); wifimanager wifi = (wifimanager) getsystemservice(context.wifi_service); wifiinfo info = wifi.getconnectioninfo(); string ssidstring = info.getssid(); //textview ssidtextview = (textview) findviewbyid(r.id.wifissid); ssidtextview = (textview) findviewbyid(r.id.wifissid); ssidtextview.settext(ssidstring); nameet = (edittext) findviewbyid(r.id.nameedit); capet = (edittext) findviewbyid(r.id.capedit); codeet = (edittext) findviewbyid(r.id.codeedit); timeet = (timepicker) findviewbyid(r.id.timeedit); bundle extras = getintent().getextras(); if (extras != null) { rowid = extras.getlong("row_id"); nameet.settext(extras.getstring("name")); capet.settext(extras.getstring("cap")); codeet.settext(extras.getstring("code")); string time = extras.getstring("time"); string[] parts = time.split(":"); timeet.setcurrenthour(integer.valueof(parts[0])); timeet.setcurrentminute(integer.valueof(parts[1])); timeet.setis24hourview(false); } button savebutton =(button) findviewbyid(r.id.savebtn); savebutton.setonclicklistener(new onclicklistener() { public void onclick(view v) { if (capet.gettext().length() != 0) { asynctask<object, object, object> savecontacttask = new asynctask<object, object, object>() { @override protected object doinbackground(object... params) { savecontact(); return null; } @override protected void onpostexecute(object result) { finish(); } }; savecontacttask.execute((object[]) null); } else { alertdialog.builder alert = new alertdialog.builder(addeditdevice.this); alert.settitle(r.string.errortitle); alert.setmessage(r.string.errormessage); alert.setpositivebutton(r.string.errorbutton, null); alert.show(); } } });} private void savecontact() { databaseconnector dbconnector = new databaseconnector(this); if (getintent().getextras() == null) { dbconnector.insertcontact(nameet.gettext().tostring(), capet.gettext().tostring(), timeet.getcurrenthour().tostring() + ":" + timeet.getcurrentminute().tostring(), codeet.gettext().tostring(), ssidtextview.gettext().tostring()); } else { dbconnector.updatecontact(rowid, nameet.gettext().tostring(), capet.gettext().tostring(), timeet.getcurrenthour().tostring() + ":" + timeet.getcurrentminute().tostring(), codeet.gettext().tostring(), ssidtextview.gettext().tostring()); } } }
source:
databaseconnector.java
import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; public class databaseconnector { private static final string db_name = "worldcountries"; private sqlitedatabase database; private databaseopenhelper dbopenhelper; public databaseconnector(context context) { dbopenhelper = new databaseopenhelper(context, db_name, null, 1); } public void open() throws sqlexception { //open database in reading/writing mode database = dbopenhelper.getwritabledatabase(); } public void close() { if (database != null) database.close(); } public void insertcontact(string name, string cap, string code, string time, string ssid) { contentvalues newcon = new contentvalues(); newcon.put("name", name); newcon.put("cap", cap); newcon.put("time", time); newcon.put("code", code); newcon.put("ssid", ssid); open(); database.insert("country", null, newcon); close(); } public void updatecontact(long id, string name, string cap, string code, string time, string ssid) { contentvalues editcon = new contentvalues(); editcon.put("name", name); editcon.put("cap", cap); editcon.put("time", time); editcon.put("code", code); editcon.put("ssid", ssid); open(); database.update("country", editcon, "_id=" + id, null); close(); } public cursor getallcontacts() { return database.query("country", new string[] {"_id", "name"}, null, null, null, null, "name"); } public cursor getonecontact(long id) { return database.query("country", null, "_id=" + id, null, null, null, null); } public void deletecontact(long id) { open(); database.delete("country", "_id=" + id, null); close(); } }
i think problem save textview local variable "ssidtextview", while should save field "ssid". change
textview ssidtextview = (textview) findviewbyid(r.id.wifissid);
into
ssid = (textview) findviewbyid(r.id.wifissid);
of course have rename "string ssid" else, "ssidstring".
Comments
Post a Comment