1

我没有完成关闭我的 Android 应用程序中的进度条。我声明了一个对象 bar 对象并获取一个调用 doInProgress 函数的字符串,并将一个名为 v1 的对象传递给它。当异步任务完成他的任务时,为什么进度条(出现 wichs)没有像 onPostExecute 方法中声明的那样被解除?

在我的活动中,我调用:

ProgressDialog pDialog;
response = new GetString(SecondActivity.this).execute(v1).get();

这是 myAsync 任务:

package com.gms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;


public class GetString extends AsyncTask<VariablesTable, Void, String> {

        private Context context;

        public ProgressDialog pDialog;

        public GetString(Context c){
            context = c;
        }           

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


        protected void onPostExecute() {
            // dismiss the dialog once got all details
            if (pDialog.isShowing()){
                pDialog.dismiss();
            }
        }


        @Override
        protected String doInBackground(VariablesTable... obj) {
            // Making HTTP request
            InputStream is = null;

            try {
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    String paramString = URLEncodedUtils.format(obj[0].cat, "utf-8");
                    obj[0].url += "?" + paramString;
                    HttpGet httpGet = new HttpGet(obj[0].url);

                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();          

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                StringBuilder sb = new StringBuilder();
                String line = null;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return sb.toString();
        }
    }

建议??提前致谢

4

3 回答 3

0

我认为问题在于您启动 ASyncTask 的方式。

response = new GetString(SecondActivity.this).execute(v1).get();

如果您从 UI 线程调用它,它将使其阻塞在那里。删除该.get()部分,以便线程可以继续运行并正常执行您的onPreExecuteonPostExecute功能。

于 2012-09-21T08:01:43.410 回答
0

chck post execute,我怀疑它是否有效

尝试将 onPostExecute() 更改为 onPostExecute(String resp)

于 2012-09-21T08:54:52.200 回答
0

在 onPostExecute() 方法中执行 System.out 以检查控件是否在此方法中到达。如果 onPost 被执行并且对话框没有关闭,那么在 UI 线程中关闭对话框 --- context.runOnUiThread 如果它仍然不起作用,则将 ProgressDialog 设为静态(不是一个解决方案,只是尝试一下),希望它现在会关闭。

于 2012-09-23T05:42:18.907 回答