Android AsyncTask AlertDialog不显示

时间:2013-06-03 14:39:01

标签: android android-asynctask alertdialog

我正在尝试在onCancelled上的AsyncTask中显示AlertDialog。我的任务正在停止,但对话框没有出现。这是我下面的代码......需要帮助。感谢...

public class getWebPage extends AsyncTask<String, Integer, String> {

protected void onPreExecute(String f) {
    // TODO Setting up variables
    f = "f";
}

protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    Looper.prepare();
    DefaultHttpClient urlClient = new DefaultHttpClient();
    HttpGet getHtml = new HttpGet(PAGE_URL);
    ResponseHandler<String> resHandler = new BasicResponseHandler();
    try {
        String htmlPage = urlClient.execute(getHtml, resHandler);
        Log.d("Html Page", htmlPage);
        confessionsPage = new File(getApplicationContext().getFilesDir(), "ConfessionsPage.html");
        if (!confessionsPage.exists()) {
            confessionsPage.createNewFile();
        }
        writer = new PrintWriter(confessionsPage, "UTF-8");
        writer.print(htmlPage.replace("<!--", "").replace("-->", ""));
        writer.flush();
        writer.close();

        Document doc = Jsoup.parse(confessionsPage, "UTF-8", "http://www.facebook.com/");
        if (doc.title().contains("Welcome to Facebook")) {
            aDialog = new AlertDialog.Builder(OpeningActivity.this).create();
            aDialog.setTitle("Restricted Access");
            aDialog.setMessage("Looks like your Confessions Page only allows login access. You may be logged in right now, but the app" +
                    " can't. Tell your page admin to allow non-logged in access for your confessions page.");
            aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    aDialog.dismiss();
                }
            });
            getWebPage.this.cancel(true);
        }

这是我取消的方法:

    @Override
protected void onCancelled() {
    // TODO Auto-generated method stub
    super.onCancelled();
    aDialog.show();
}

1 个答案:

答案 0 :(得分:0)

尝试移动从doInBackgroundonCancelled的所有内容。

这将是您的onCancelled

AlertDialog aDialog = new AlertDialog.Builder(OpeningActivity.this).create();
aDialog.setTitle("Restricted Access");
aDialog.setMessage("Looks like your Confessions Page only allows login access. You may be logged in right now, but the app" +
                        " can't. Tell your page admin to allow non-logged in access for your confessions page.");
aDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       aDialog.dismiss();
   }
});
aDialog.show();

并删除doInBackground中的所有内容。

或者,您也可以将其添加到AsyncTask的{​​{1}}中,并将任务的结果类型更改为onPostExecute。在Boolean中查看结果。如果它是假的,您需要显示此错误。从那里显示它。 请注意,onPostExecute由UI线程执行,因此您不需要onPostExecute内容。