마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

Asynctask 사용중 ProgressDialog가 안보입니다.

0 추천
public class TestActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        restAPIAsyncTask async= new restAPIAsyncTask(getApplicationContext());

        async.execute("Good");
        try {
            async.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    public class restAPIAsyncTask extends AsyncTask<String, Void, Boolean> {
        ProgressDialog pDialog = null;
        Context aContext;

        public restAPIAsyncTask(Context context) {
            aContext = context;
            pDialog = new ProgressDialog(aContext);
        }

        @Override
        protected void onPreExecute() {
            Log.d("Log", "onPreExecute");
            pDialog.setMessage("Please Wait....");
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            String url = "http://example.com";
            List<NameValuePair> postParams = new ArrayList<NameValuePair>();

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);

            postParams.add(new BasicNameValuePair("type", "all"));
            postParams.add(new BasicNameValuePair("limit", "all"));

            try {
                httppost.setEntity(new UrlEncodedFormEntity(postParams));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }


            try {
                HttpResponse response = httpclient.execute(httppost);
                Log.d("Log", "response:" + EntityUtils.toString(response.getEntity()));
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
             // if (pDialog != null)
            //     pDialog.dismiss();
        }
    }
}

 

아예 보이지 않고, onPostExecute에서 dismiss 주석을 해제하면 에러가 납니다. 

뭐가 잘못된건까요??

로켓군 (540 포인트) 님이 2015년 4월 9일 질문

2개의 답변

+1 추천
 
채택된 답변

restAPIAsyncTask async= new restAPIAsyncTask(getApplicationContext())

applicationcontext로는 UI관련 작업이 안될겁니다.

익명사용자 님이 2015년 4월 9일 답변
로켓군님이 2015년 4월 10일 채택됨
감사합니다. getApplicationContext() -> this로 수정후 정상 작동됩니다. 이부분은 추후에 조금 더 공부해야겠네요.
+1 추천

restAPIAsyncTask async= new restAPIAsyncTask(getApplicationContext());

restAPIAsyncTask async= new restAPIAsyncTask(this);

 

로 바꿔보세요.

그리고 class 첫글자는 대문자로.. :)

mamondebaltob (32,750 포인트) 님이 2015년 4월 9일 답변
...