vendredi 30 mai 2014

Cannot show progress dialog in background AsyncTask


Vote count:

0




I have an AsyncTask I am trying to run to query data from a webservice, displaying a progress dialog while it does, however I get an exception when I try and show the progress dialog. THe line that errors is in onPreExecute() -> pDialog.show()


The call to launch the task from my activity (which extends Activity)



new GetData(getApplicationContext())
.execute("11");


The Async class



public class GetData extends AsyncTask<String, Integer, ArrayList<StorySeed>>
{
private final static String EMPLOYEE_SERVICE_URI = "http://localhost:82/EmployeeService_deploy/EmployeeInfo.svc/GetEmployee/?key=";
private ProgressDialog pDialog;
private Context context;

public GetData(Context cxt)
{
context = cxt;
}

@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading data...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.show();
}

@Override
protected void onProgressUpdate(Integer... progress)
{
super.onProgressUpdate(progress);
pDialog.setProgress(progress[0]);
}

protected ArrayList<StorySeed> doInBackground(String... params)
{
ArrayList<StorySeed> storySeeds = new ArrayList<StorySeed>();

DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(EMPLOYEE_SERVICE_URI + "11");

request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");

//get the response
try
{
HttpResponse response = client.execute(request);

HttpEntity entity = response.getEntity();

if(entity.getContentLength() != 0) {
// stream reader object
Reader employeeReader = new InputStreamReader(response.getEntity().getContent());
char[] buffer = new char[(int) response.getEntity().getContentLength()];
employeeReader.read(buffer);
employeeReader.close();

JSONObject employee = new JSONObject(new String(buffer));

storySeeds.add(new StorySeed(employee.getString("FirstName"), 34, employee.getString("Address"), R.drawable.profile, R.drawable.camera, "What does this photo make you think of?", R.drawable.ocean));
}
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}

return storySeeds;
}

protected void onPostExecute(ArrayList<StorySeed> storySeeds)
{
pDialog.dismiss();
}
}


LogCat



05-30 05:39:30.280: E/AndroidRuntime(1442): FATAL EXCEPTION: main
05-30 05:39:30.280: E/AndroidRuntime(1442): Process: lakecrest.tayle, PID: 1442
05-30 05:39:30.280: E/AndroidRuntime(1442): java.lang.RuntimeException: Unable to start activity ComponentInfo{lakecrest.tayle/lakecrest.tayle.FindTayleActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.os.Handler.dispatchMessage(Handler.java:102)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.os.Looper.loop(Looper.java:136)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-30 05:39:30.280: E/AndroidRuntime(1442): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 05:39:30.280: E/AndroidRuntime(1442): at java.lang.reflect.Method.invoke(Method.java:515)
05-30 05:39:30.280: E/AndroidRuntime(1442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-30 05:39:30.280: E/AndroidRuntime(1442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-30 05:39:30.280: E/AndroidRuntime(1442): at dalvik.system.NativeStart.main(Native Method)
05-30 05:39:30.280: E/AndroidRuntime(1442): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.app.Dialog.show(Dialog.java:286)
05-30 05:39:30.280: E/AndroidRuntime(1442): at lakecrest.tayle.network.tasks.GetData.onPreExecute(GetData.java:42)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.os.AsyncTask.execute(AsyncTask.java:535)
05-30 05:39:30.280: E/AndroidRuntime(1442): at lakecrest.tayle.FindTayleActivity.loadStorySeeds(FindTayleActivity.java:65)
05-30 05:39:30.280: E/AndroidRuntime(1442): at lakecrest.tayle.FindTayleActivity.onCreate(FindTayleActivity.java:31)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.app.Activity.performCreate(Activity.java:5231)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-30 05:39:30.280: E/AndroidRuntime(1442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)


asked 2 mins ago


1 Answer



Vote count:

0




Use Activity Context



new GetData(ActivityName.this).execute("11");


Read


When to call activity context OR application context?



answered 30 secs ago





Aucun commentaire:

Enregistrer un commentaire