android - can some 1 point out the null pointer exception -
i have edited code , null pointer error pointing inside loop less errors before still dont't know why nullpointer called.
public class searchlist extends listactivity { @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); new loadsomestuff().execute(); } public class loadsomestuff extends asynctask<string, integer, string[]> { progressdialog dialog; protected void onpreexecute() { dialog = new progressdialog(searchlist.this); dialog.setprogressstyle(progressdialog.style_spinner); dialog.setmax(100); dialog.show(); } @override protected string[] doinbackground(string... arg0) { // todo auto-generated method stub for(int =0; i<20; i++) { publishprogress(5); try { thread.sleep(80); } catch(interruptedexception e) { e.printstacktrace(); } } dialog.dismiss(); int loops = search_page.returnlooped(); int[] teacup = search_page.returnnumarray(); sqlstuff searching = new sqlstuff(searchlist.this); string[] idsysnames = searching.getidsysname(); searching.close(); string[] resultlist = new string[loops]; for(int i=0; < loops; i++ ) { if(idsysnames[teacup[i]] != null) { resultlist[i].equals(idsysnames[teacup[i]]); } } setlistadapter(new arrayadapter<string>(searchlist.this, android.r.layout.simple_list_item_1, resultlist)); return null; } protected void onprogressupdate(integer...progress) { dialog.incrementprogressby(progress[0]); } } @override public void onlistitemclick(listview l, view v, int position, long id){ //todo auto-generated method stub super.onlistitemclick(l, v, position, id); //string pos = resultlist[position]; intent ourintent = new intent("com.mc.chempal.result2"); startactivity(ourintent); } }
the method used within code above below .
public string[] getidsysname() { string[] result = new string[0]; try { string[] columns = new string[] {key_sysname}; cursor c = chempal.query(database_table, columns, null, null, null, null, null); log.d("sqldstuff", "cursor count: "+c.getcount()); int isysname = c.getcolumnindex(key_sysname); int = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ i++; } result = new string[i]; = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ result[i] = c.getstring(isysname); i++; } } catch(exception e) { } return result; }
you can't
int loops = search_page.returnlooped(); int[] teacup = search_page.returnnumarray();
before oncreate()
, because you're trying access application resources still not initialized (hence npes). can see here, cannot assume activity launched until oncreate()
called.
the entire lifetime of activity happens between first call oncreate(bundle) through single final call ondestroy().
as general rule: shouldn't anything before oncreate()
called.
also should not db reading in oncreate()
because run ui thread, , might anr if db access long. see asynctasks tasks on background (you might think progressdialog when loading data if gui needs data right away).
Comments
Post a Comment