无法在后台AsyncTask中显示进度对话框

时间:2014-05-30 09:45:45

标签: android android-asynctask

我有一个AsyncTask我试图运行以查询来自Web服务的数据,同时显示进度对话框,但是当我尝试显示进度对话框时,我得到一个异常。错误的行在onPreExecute() - > pDialog.show()

从我的活动(扩展活动)启动任务的调用

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

Async类

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)

2 个答案:

答案 0 :(得分:2)

使用活动上下文

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

When to call activity context OR application context?

另请阅读以下内容

"android.view.WindowManager$BadTokenException: Unable to add window" on buider.show()

答案 1 :(得分:0)

尝试在创建ProgressDialog时传递活动

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