我正在尝试在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();
}
答案 0 :(得分:0)
尝试移动从doInBackground
到onCancelled
的所有内容。
这将是您的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
内容。