android - Error in populating data to listView -
i trying access data sqlite , try populate on listview. got error
the following java code:
package com.example.cmas; import java.util.arraylist; import org.w3c.dom.text; import android.app.activity; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.button; import android.widget.listview; import android.widget.textview; import android.widget.toast; import static com.example.cmas.mysqliteopenhelper.*; public class surveyresult extends activity{ //listview lstsurvey; sqlitedatabase profile; cursor cur; arraylist<candidate> candidates; @override protected void oncreate(bundle savedinstancestate) { setcontentview(r.layout.surveylist_main); initilizer(); super.oncreate(savedinstancestate); } void initilizer(){ profile = new mysqliteopenhelper(this,database_name,null,1).getreadabledatabase(); cur = profile.query(table_name, new string[]{col_name,col_name,col_floor,col_area,col_lat,col_long,col_photo}, null, null, null, null,null); candidates = new arraylist<candidate>(); if(cur!=null){ if(!cur.isafterlast()) cur.movetofirst(); do{ candidates.add(new candidate(cur.getstring(0),cur.getint(1),cur.getfloat(2),cur.getfloat(3),cur.getfloat(4),cur.getstring(5))); cur.movetonext(); }while(!cur.isafterlast()); cur.close(); } button btnback = (button) findviewbyid(r.id.btnback); listview lstcandidates = (listview) findviewbyid(r.id.lstcandidates); lstcandidates.setadapter(new customview(this)); btnback.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { surveyresult.this.finish(); } }); } public class customview extends baseadapter{ context localcontext; public customview(context loacontext){ super(); this.localcontext = localcontext; } @override public int getcount() { // todo auto-generated method stub return 0; } @override public object getitem(int position) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { toast.maketext(getbasecontext(), "herview", toast.length_short).show(); view myview; if(convertview==null){ myview = new view(localcontext); layoutinflater myinflater = getlayoutinflater(); myview = myinflater.inflate(r.layout.surveylist_main, parent, false); } else{ myview=convertview; } //need populate values textview txtname = (textview) myview.findviewbyid(r.id.txtname); textview nofloors = (textview) myview.findviewbyid(r.id.txtfloors); textview plotarea = (textview) myview.findviewbyid(r.id.txtarea); textview lat = (textview) myview.findviewbyid(r.id.txtlat); textview lng = (textview) myview.findviewbyid(r.id.txtlong); textview photo = (textview) myview.findviewbyid(r.id.txtphoto); txtname.settext("building name:"+candidates.get(position).getname()); nofloors.settext("no of floors:"+candidates.get(position).getnooffloors()); plotarea.settext("area:"+candidates.get(position).getarea()); lat.settext("latitude:"+candidates.get(position).getlat()); lng.settext("longitude:"+candidates.get(position).getlng()); photo.settext("photo:"+candidates.get(position).getphoto()); return myview; } } }
while running app got following errors:
03-10 04:27:17.825: e/androidruntime(1736): fatal exception: main 03-10 04:27:17.825: e/androidruntime(1736): java.lang.runtimeexception: unable start activity componentinfo{com.example.cmas/com.example.cmas.surveyresult}: java.lang.nullpointerexception 03-10 04:27:17.825: e/androidruntime(1736): @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) 03-10 04:27:17.825: e/androidruntime(1736): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 03-10 04:27:17.825: e/androidruntime(1736): @ android.app.activitythread.access$600(activitythread.java:141) 03-10 04:27:17.825: e/androidruntime(1736): @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 03-10 04:27:17.825: e/androidruntime(1736): @ android.os.handler.dispatchmessage(handler.java:99) 03-10 04:27:17.825: e/androidruntime(1736): @ android.os.looper.loop(looper.java:137) 03-10 04:27:17.825: e/androidruntime(1736): @ android.app.activitythread.main(activitythread.java:5103) 03-10 04:27:17.825: e/androidruntime(1736): @ java.lang.reflect.method.invokenative(native method) 03-10 04:27:17.825: e/androidruntime(1736): @ java.lang.reflect.method.invoke(method.java:525) 03-10 04:27:17.825: e/androidruntime(1736): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 03-10 04:27:17.825: e/androidruntime(1736): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 03-10 04:27:17.825: e/androidruntime(1736): @ dalvik.system.nativestart.main(native method) 03-10 04:27:17.825: e/androidruntime(1736): caused by: java.lang.nullpointerexception 03-10 04:27:17.825: e/androidruntime(1736): @ com.example.cmas.surveyresult.initilizer(surveyresult.java:62) 03-10 04:27:17.825: e/androidruntime(1736): @ com.example.cmas.surveyresult.oncreate(surveyresult.java:34) 03-10 04:27:17.825: e/androidruntime(1736): @ android.app.activity.performcreate(activity.java:5133) 03-10 04:27:17.825: e/androidruntime(1736): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-10 04:27:17.825: e/androidruntime(1736): @ android.app.activitythread.performlaunchactivity(activitythread.java:2175) 03-10 04:27:17.825: e/androidruntime(1736): ... 11 more 03-10 04:28:20.875: i/process(1736): sending signal. pid: 1736 sig: 9
while viewing page got error.
i think should open database before using in query. try using
profile.open();
before running
cur = profile.query(table_name, new string[] {col_name,col_name,col_floor,col_area,col_lat,col_long,col_photo}, null, null, null, null,null);
Comments
Post a Comment