Android AsyncTask AlertDialog未显示

Android AsyncTask AlertDialog未显示,android,android-asynctask,android-alertdialog,Android,Android Asynctask,Android Alertdialog,我试图在onCancelled上的AsyncTask中显示AlertDialog。我的任务正在正确停止,但对话框未出现。下面是我的代码。。。我需要帮助。谢谢 public class getWebPage extends AsyncTask<String, Integer, String> { protected void onPreExecute(String f) { // TODO Setting up variables f = "f"; } protec

我试图在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);
        }

尝试将所有内容从
doInBackground
移动到
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
onPostExecute
中,并将任务的结果类型更改为
布尔值。在
onPostExecute
中检查结果。如果为false,则需要显示此错误。从那里显示它。
请注意,
onPostExecute
由UI线程执行,因此您不需要
runOnUiThread
之类的东西。

要从另一个线程执行UI操作,您需要使用父线程的
post()
方法view@StinePike是的,我错了。它已经在UI线程上。但问题可能是您必须从UI线程设置所有AlertDialog选项。顺便说一句,我看不出你在代码中构建了
adilog
。adilog与所有其他变量一起构建在Activity类的开头,在UI线程中,我尝试将所有内容都放在onCancelled中,但没有起作用。我不能把它放在执行后。我放置取消代码的原因是它永远不会到达postExecute。有一些原因它不会到达postExecute。AsyncTask对函数名以及是否重写原始函数非常敏感。当返回类型为布尔值时,它应该是带有
@Override
注释的
public void onPostExecute(布尔结果)
,否则它不会执行它。但我不希望它到达postExecute。我想让它停在中间。我不想运行
adilog
之后的代码。
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();