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;
}
....
}
ERRO
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
Pergunta:
Como passo o contexto do método da onde eu to para o adapter que recebe o contexto?
Antes de carregar o Json, eu tinha criado outra classe, e quando eu passava o this como parâmetro, funcionava corretamente. Agora nesse outro método não funciona.