0

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

1 回答 1

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.

或者,您也可以将其放入您AsyncTaskonPostExecute中,并将任务的结果类型更改为Boolean. 在onPostExecute检查结果。如果它是假的,你需要显示这个错误。从那里显示它。请注意,这onPostExecute是由 UI 线程执行的,因此您不需要这些runOnUiThread东西。

于 2013-06-03T14:48:55.723 回答