php - Android App crashes citing an WindowManager error when trying to get values from database -


basically i've got android app , mysql database , i'm using php try , populate listview entries it. reason following code seems crash , logcat says there's issue windowmanager

the code is:

allcomediansactivity.java:

package com.example.connecttest;  public class allcomediansactivity extends listactivity {      // progress dialog     private progressdialog pdialog;      // creating json parser object     jsonparser jparser = new jsonparser();      arraylist<hashmap<string,string>> comedianlist;      // url comedian names     private static string url_all_comedians = "http://localhost/connect/get_all_comedians.php";      //json node names     private static final string tag_success = "success";     private static final string tag_comedian = "comedian";     private static final string tag_pid = "id";     private static final string tag_name = "name";      // products jsonarray     jsonarray comedians = null;      public void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);         setcontentview(r.layout.all_comedians);          //hashmap listview         comedianlist = new arraylist<hashmap<string, string>>();          //loading comedians in background thread         new loadallcomedians().execute();          // listview         listview lv = getlistview();          lv.setonitemclicklistener(new onitemclicklistener(){              public void onitemclick(adapterview<?> parent, view view, int position, long id){                 // getting values selected listitem                 string pid = ((textview) view.findviewbyid(r.id.id)).gettext().tostring();                  //             }          });     }      class loadallcomedians extends asynctask<string, string, string>{          // before starting background thread show progress dialog         protected void onpreexecute(){             super.onpreexecute();             pdialog = new progressdialog(allcomediansactivity.this);             pdialog.setmessage("loading products. please wait...");             pdialog.setindeterminate(false);             pdialog.setcancelable(false);             pdialog.show();         }          @override         protected string doinbackground(string... args) {             // building parameters             list<namevaluepair> params = new arraylist<namevaluepair>();             // getting json string url             jsonobject json = jparser.makehttprequest(url_all_comedians, "get", params);              //check log cat json response             log.d("all comedians: ", json.tostring());              try{                 //checking success tag in json                 int success = json.getint(tag_success);                  if(success == 1){                     // comedians found                     // getting array of comedians                     comedians = json.getjsonarray(tag_comedian);                      // looping through comedians                     for(int = 0; < comedians.length(); i++){                         jsonobject c = comedians.getjsonobject(i);                          // storing each json item in variable                         string id = c.getstring(tag_pid);                         string name = c.getstring(tag_name);                          // creating new hashmap                         hashmap<string, string> map = new hashmap<string, string>();                          // adding each child node hashmap key => value                         map.put(tag_pid, id);                         map.put(tag_name,name);                          // adding hashlist arraylist                         comedianlist.add(map);                                           }                 } else {                     // no products found                     // launch add new comedian activity                     intent = new intent(getapplicationcontext(), newcomedianactivity.class);                     // closing previous activities                     i.addflags(intent.flag_activity_clear_top);                     startactivity(i);                 }             } catch (jsonexception e){                 e.printstacktrace();             }              return null;         }          // after completing background task dismiss progress dialog          protected void onpostexecute(string file_url){             // dismiss dialog after getting comedians             pdialog.dismiss();             // updating ui background thread             runonuithread(new runnable(){                 public void run(){                     // updating json data listview                     listadapter adapter = new simpleadapter(allcomediansactivity.this, comedianlist, r.layout.list_item, new string[] {tag_pid, tag_name}, new int[] {r.id.id,r.id.name});                     setlistadapter(adapter);                 }             });         }      }  } 

i have correct imports removed them sake of brevity.

the jsonparser references is:

jsonparser.java:

package com.example.connecttest;  public class jsonparser {      static inputstream = null;     static jsonobject jobj = null;     static string json = "";      // constructor     public jsonparser() {      }      // function json url     // making http post or method     public jsonobject makehttprequest(string url, string method,             list<namevaluepair> params) {          // making http request         try {              // check request method             if(method == "post"){                 // request method post                 // defaulthttpclient                 defaulthttpclient httpclient = new defaulthttpclient();                 httppost httppost = new httppost(url);                 httppost.setentity(new urlencodedformentity(params));                  httpresponse httpresponse = httpclient.execute(httppost);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();              }else if(method == "get"){                 // request method                 defaulthttpclient httpclient = new defaulthttpclient();                 string paramstring = urlencodedutils.format(params, "utf-8");                 url += "?" + paramstring;                 httpget httpget = new httpget(url);                  httpresponse httpresponse = httpclient.execute(httpget);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();             }                     } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          try {             bufferedreader reader = new bufferedreader(new inputstreamreader(                     is, "iso-8859-1"), 8);             stringbuilder sb = new stringbuilder();             string line = null;             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             json = sb.tostring();         } catch (exception e) {             log.e("buffer error", "error converting result " + e.tostring());         }          // try parse string json object         try {             jobj = new jsonobject(json);         } catch (jsonexception e) {             log.e("json parser", "error parsing data " + e.tostring());         }          // return json string         return jobj;      } } 

and php file returns following information when navigate in browser:

{"comedian":[{"id":"1","name":"mike coombes","address":"test","email":"test@test.com","tel":"xxxxxxxx"},{"id":"2","name":"test","address":"test","email":"tester@test.com","tel":"xxxxxxx"}],"success":1} 

so php querying database correctly , returning using json.

now debugging program showed seems far following line:

 jsonobject json = jparser.makehttprequest(url_all_comedians, "get", params); 

before comes unstuck, i've been pulling hair out hours trying work no avail, wondering if people me work through problem on here!

as asked logcat errors are:

03-21 05:57:10.319: e/buffer error(3500): error converting result java.lang.nullpointerexception: lock == null 03-21 05:57:10.319: e/json parser(3500): error parsing data org.json.jsonexception: end of input @ character 0 of  03-21 05:57:10.329: e/androidruntime(3500): fatal exception: asynctask #1 03-21 05:57:10.329: e/androidruntime(3500): java.lang.runtimeexception: error occured while executing doinbackground() 03-21 05:57:10.329: e/androidruntime(3500):     @ android.os.asynctask$3.done(asynctask.java:299) 03-21 05:57:10.329: e/androidruntime(3500):     @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 03-21 05:57:10.329: e/androidruntime(3500):     @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 03-21 05:57:10.329: e/androidruntime(3500):     @ java.util.concurrent.futuretask.run(futuretask.java:239) 03-21 05:57:10.329: e/androidruntime(3500):     @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 03-21 05:57:10.329: e/androidruntime(3500):     @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 03-21 05:57:10.329: e/androidruntime(3500):     @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 03-21 05:57:10.329: e/androidruntime(3500):     @ java.lang.thread.run(thread.java:856) 03-21 05:57:10.329: e/androidruntime(3500): caused by: java.lang.nullpointerexception 03-21 05:57:10.329: e/androidruntime(3500):     @ com.example.connecttest.allcomediansactivity$loadallcomedians.doinbackground(allcomediansactivity.java:93) 03-21 05:57:10.329: e/androidruntime(3500):     @ com.example.connecttest.allcomediansactivity$loadallcomedians.doinbackground(allcomediansactivity.java:1) 03-21 05:57:10.329: e/androidruntime(3500):     @ android.os.asynctask$2.call(asynctask.java:287) 03-21 05:57:10.329: e/androidruntime(3500):     @ java.util.concurrent.futuretask.run(futuretask.java:234) 03-21 05:57:10.329: e/androidruntime(3500):     ... 4 more 

i managed working in end had 2 problems

switched localhost 10.0.2.2 , had add protocol url

also had typo permission , had change users-permission uses-permission working fine now


Comments