database - Print Data from Array to Viewlist Android -
i'm trying data sqlite table view list, when try connect database, have error: 05-02 13:59:18.123: e/androidruntime(714): java.lang.runtimeexception: unable start activity componentinfo{com.example.add/admed.listar}: java.lang.nullpointerexception
the code of db connector is:
private static final string db_name = "admed"; private sqlitedatabase database; private basedados basedados; public basedadosconect(context context) { basedados = new basedados(context, db_name, null, 1); } public void open() throws sqlexception { //open database in reading/writing mode database = basedados.getwritabledatabase(); } public boolean isopen(){ if (database!=null){ return true; } else{ return false; } } public void close() { if (database != null) database.close(); }
the code of class want print data (listar.java):
basedadosconect db; protected void oncreate(bundle savedinstancestate) { intent menu = getintent(); string tipo = menu.getstringextra("tipo"); super.oncreate(savedinstancestate); if (tipo.equals("medicamentos")) { setcontentview(r.layout.medicamentos); db.open(); db.insertmed(1,"trolax","trolax",2); db.insertmed(2,"trolex","trolex",3); cursor cursor = db.getallmeds(); string[] values = new string[cursor.getcount()]; if (cursor.movetofirst()) { (int = 0; <= cursor.getcount(); i++) { values[i] = cursor.getstring(2); cursor.movetonext(); } } db.close(); final listview listview = (listview) findviewbyid(r.id.listview1); final arraylist<string> list = new arraylist<string>(); (int = 0; < values.length; ++i) { list.add(values[i]); } final stablearrayadapter adapter = new stablearrayadapter(this, android.r.layout.simple_list_item_1, list); listview.setadapter(adapter); button medicamentos = (button) findviewbyid(r.id.button1); medicamentos.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent medsintent = new intent(view.getcontext(), editar.class); medsintent.putextra("tipo", "medicamentos"); medsintent.putextra("accao", "adicionar"); startactivity(medsintent); } }); } button voltar = (button) findviewbyid(r.id.button2); voltar.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent myintent = new intent(view.getcontext(), mainactivity.class); startactivityforresult(myintent, 0); } }); } private class stablearrayadapter extends arrayadapter<string> { hashmap<string, integer> midmap = new hashmap<string, integer>(); public stablearrayadapter(context context, int textviewresourceid, list<string> objects) { super(context, textviewresourceid, objects); (int = 0; < objects.size(); ++i) { midmap.put(objects.get(i), i); } } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.string.app_name, menu); return true; }
getallmeds() code:
public cursor getallmeds() { return database.query("medicamentos", new string[] {"medicamentos_desig"}, null, null, null, null, null); }
full log:
05-02 13:59:18.113: w/dalvikvm(714): threadid=1: thread exiting uncaught exception (group=0x40015560) 05-02 13:59:18.123: e/androidruntime(714): fatal exception: main 05-02 13:59:18.123: e/androidruntime(714): java.lang.runtimeexception: unable start activity componentinfo{com.example.add/admed.listar}: java.lang.nullpointerexception 05-02 13:59:18.123: e/androidruntime(714): @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 05-02 13:59:18.123: e/androidruntime(714): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 05-02 13:59:18.123: e/androidruntime(714): @ android.app.activitythread.access$1500(activitythread.java:117) 05-02 13:59:18.123: e/androidruntime(714): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 05-02 13:59:18.123: e/androidruntime(714): @ android.os.handler.dispatchmessage(handler.java:99) 05-02 13:59:18.123: e/androidruntime(714): @ android.os.looper.loop(looper.java:123) 05-02 13:59:18.123: e/androidruntime(714): @ android.app.activitythread.main(activitythread.java:3683) 05-02 13:59:18.123: e/androidruntime(714): @ java.lang.reflect.method.invokenative(native method) 05-02 13:59:18.123: e/androidruntime(714): @ java.lang.reflect.method.invoke(method.java:507) 05-02 13:59:18.123: e/androidruntime(714): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 05-02 13:59:18.123: e/androidruntime(714): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 05-02 13:59:18.123: e/androidruntime(714): @ dalvik.system.nativestart.main(native method) 05-02 13:59:18.123: e/androidruntime(714): caused by: java.lang.nullpointerexception 05-02 13:59:18.123: e/androidruntime(714): @ admed.listar.oncreate(listar.java:34) 05-02 13:59:18.123: e/androidruntime(714): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 05-02 13:59:18.123: e/androidruntime(714): @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 05-02 13:59:18.123: e/androidruntime(714): ... 11 more
what i'm doing wrong? in advance!
you calling non static method of class. so, need initialize class object before calling member function of class. object declaration alone not enough.
insert line before db.open(); in oncreate() of listar.java.
db = new basedadosconect(this);
Comments
Post a Comment