1
private class GetEmpresas extends AsyncTask<Void, Void, Void> {
...
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
//Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
empresasJson = jsonObj.getJSONArray(TAG_EMPRESAS);
// looping through All Contacts
for (int i = 0; i < empresasJson.length(); i++) {
JSONObject c = empresasJson.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NOME);
String cidade = c.getString(TAG_CIDADE);
String endereco = c.getString(TAG_ENDERECO);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NOME, name);
contact.put(TAG_CIDADE, cidade);
contact.put(TAG_ENDERECO, endereco);
empresas = new ArrayList<Empresas>();
Empresas item = new Empresas(name,endereco, R.drawable.logo);
empresas.add(item);
adapter = new AdapterEmpresas(empresas, ?????);
listView.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
....
}
ERROR
01-30 18:21:25.058: E/AndroidRuntime(8276): FATAL EXCEPTION: AsyncTask #1
01-30 18:21:25.058: E/AndroidRuntime(8276): java.lang.RuntimeException: An error occured while executing doInBackground()
01-30 18:21:25.058: E/AndroidRuntime(8276): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-30 18:21:25.058: E/AndroidRuntime(8276): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-30 18:21:25.058: E/AndroidRuntime(8276): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-30 18:21:25.058: E/AndroidRuntime(8276): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-30 18:21:25.058: E/AndroidRuntime(8276): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-30 18:21:25.058: E/AndroidRuntime(8276): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-30 18:21:25.058: E/AndroidRuntime(8276): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-30 18:21:25.058: E/AndroidRuntime(8276): at java.lang.Thread.run(Thread.java:856)
01-30 18:21:25.058: E/AndroidRuntime(8276): Caused by: java.lang.NullPointerException
01-30 18:21:25.058: E/AndroidRuntime(8276): at com.solutudo.activities.Main$GetEmpresas.doInBackground(Main.java:137)
01-30 18:21:25.058: E/AndroidRuntime(8276): at com.solutudo.activities.Main$GetEmpresas.doInBackground(Main.java:1)
01-30 18:21:25.058: E/AndroidRuntime(8276): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-30 18:21:25.058: E/AndroidRuntime(8276): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-30 18:21:25.058: E/AndroidRuntime(8276): ... 4 more
Question:
How do I pass the context of the method Adapter that gets the context?
Before loading the Json, I had created another class, and when I passed the this as a parameter, it worked correctly. Now in this other method it does not work.
Legal Francisco, thanks for the information. Now I put the filling of the list in Postexecute and it worked. Also missed, I have started Listview with findviewById, I did all this and is working now. Thanks anyway.
– Artur